Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Threads help !!! 1

Status
Not open for further replies.

poolboy

MIS
Jan 25, 2002
44
GB
I'm a bit confused on join() in threads. I know what it does but it is the actual implemetation that's got me confused..I know it pauses the current thread to let the thread on which join() is called terminates...
so for example if I have threadA and threadB...

could you pleease provide a very simple line of code to show how to apply join() if threadA were to wait for threadB to finish !!

Regards
poolboy
 
If you're trying to do something like ...
Code:
   threadA.join(threadB)   // No chance! [won't compile].
... then that's not the way it works. Instead, in the code for
Code:
threadA
(i.e. in it's
Code:
run()
method), you make a call like...
Code:
   threadB.join();
... (Assuming that threadA knows about threadB). Alternatively, if you had two threads created in you
Code:
main
program, for example...
Code:
   Thread threadA, threadB;
   threadA= new CountDownThread("Count-Down-Ten", 10);
   threadB= new CountDownThread("Count-Down-Twenty", 20);
... you could then start them both off,...
Code:
   threadA.start();
   threadB.start();
... and then make sure that they both finish before you continue any further in your
Code:
main
thread...
Code:
   try{
      threadA.join();
      threadB.join();
   }catch(InterruptedException ie){
      // Thread was Interrupted during join
   }

   blastOff();
... And it doesn't matter which one finishes first, because
Code:
blastOff()
wont get called until both threads finish (unless one of the threads happens to be Interrupted).

s-)
StevieK
 
Hi Steviek
thanks a million !!
one more thing...in your code below :

***********"in the code for threadA (i.e. in it's run() method), you make a call like...

threadB.join();

... (Assuming that threadA knows about threadB). "**********

when you call the join() on threadB as above
does it imply that threadA must pause for threadB to complete or is it the other way round..

Thanks
 
When
Code:
threadA
calls
Code:
threadB.join()
it then
Code:
wait
s (forever) until threadB dies (meaning that threadB has got to the end of its
Code:
run()
method).

So, if
Code:
threadA
was doing something like...
Code:
   lightMatch();

   try{
      threadB.join();
   }catch(InterruptedException ie){
   }

   igniteTouchPaper();
... Then threadA will not be able to call
Code:
igniteTouchPaper()
until either
Code:
threadB
dies or
Code:
threadB
is
Code:
interrupt
ed.

If, instead, you want
Code:
threadA
to be able to keep doing things until
Code:
threadB
dies (such as counting how long it's taking, and maybe letting the user know that something is happening), you can call
Code:
join()
will some arguments, telling it that it should only
Code:
wait
a maximum amount of milliseconds (and even nanoseconds, if you want).

So you could do something like...
Code:
   numberOfSecondsWeHaveWaitedForThreadBToDie= -1;

   for(;threadB.isAlive();)
   {
      numberOfSecondsWeHaveWaitedForThreadBToDie++;
      try{
         threadB.join(1000);
      }catch(InterruptedException ie){
      }
   }
... The only benefit of this over polling the
Code:
threadB.isAlive()
method is that when
Code:
threadB
eventually dies,
Code:
threadA
could potentially start executing immediately, as opposed to
Code:
sleep
ing until the next time it decides to poll.

Hope that was what you were after
StevieK s-)
 
StevieK,
it was much more than I was after and I couldn't be more grateful to you. Thanks you very much !!

Regards
poolboy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top