• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use join( )

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this program , I want to join t2 thread ( fast one ) into t1 or in other word I want that t1 should finish first & then t2 .

how can I do this ?

thanks .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want t2 to join t1 or do you want the application to join t1 before it even starts t2?

If the former you need to have a reference to t1 inside t2 and call join in t2.
If the latter, you call t1.join(); before you call t2.start();
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
Do you want t2 to join t1 or do you want the application to join t1 before it even starts t2?

If the former you need to have a reference to t1 inside t2 and call join in t2.
If the latter, you call t1.join(); before you call t2.start();


application means main thread , right . so yes I can join it like t1.join() after t1.start() , that is solving my purpose ... because now t2 will start execution after t1 will get completed .


Jeroen , can you explain first one with some code .. please
thanks a lot .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure.
You'll need to either modify your Runnable a bit first though.



Now t2 will wait until t1 is done. t1 will wait for nothing because its waitFor Thread is null.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
superb logic
thanks a lot .
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jaroen ,

Can you please extends this code to use wait() , notify() & notifyAll() mrthod & explain ... please .

thanks a lot .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic