Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and 1.4 exam

 
Mansi Dave
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Do we need to know Thread.join() for the 1.4 exam?
Thanks.
Mansi
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming so. I read from the feedback from those who took 1.4 that test included 8-10 heavy-duty thread questions....
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's not much to know about join. It blocks waiting for the thread you're joining to finish running.
 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This program shows the working of the ThreadObject.join() method. The join() method forces a thread to wait for another thread to die before it is allowed to continue executing. Or, put in another way, thread A may call thread B’s join() method if A wants to stop executing and wait for thread B to finish before A continues.
Without t.join() – notice that t’s count is after main’s begin and end. This is because both the main and t threads execute independent of each other and since t sleeps, the main thread has plenty of CPU time to finish executing.

With t.join() - The output shows that T’s counting activity is between main’s begin and end. Now main waits for t to die 1st before continuing – because main calls t.join(). By calling this method, main tells the t thread that “I will join after you !”

Some extra experimenting :
Call t.join() before calling t.start() – This means main is telling t that it wants to join at the end of t’s execution EVEN BEFORE t STARTED EXECUTING. t.join() will have no effect on main. Maybe t replies : “you may continue since I have not even started executing… do not wait for me.” Join only makes main wait if t is already started.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic