• Post Reply Bookmark Topic Watch Topic
  • New Topic

thread Join method  RSS feed

 
inmank Kar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning threads in java. This is a simple example for Join method
Based on my understanding of the join method, when a Join method is called on a thread t1 the current thread will block its execution untill the thread t1 completes its action. Based on this understanding I have written the following code. But its not working as expected. Kindly correct me If my understanding is wrong. Please find my code below, from the code I have 3 threads t1, t2 & t3. I have started t1 and t2 and then call join on t2 and starting t1. When t2. join is called, I expect thread t1 to wait but its running in parallel.



Ouput:

Running in Thread1 1
Running in Thread2 1
Running in Thread1 2
Running in Thread2 2
Running in Thread1 3
Running in Thread2 3
Running in Thread2 4
Running in Thread1 4
Running in Thread1 5
Running in Thread2 5
Running in Thread3 1
Running in Thread3 2
Running in Thread3 3
Running in Thread3 4
Running in Thread3 5
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Inmank,

you write:
inmank Kar wrote:(...), from the code I have 3 threads t1, t2 & t3. I have started t1 and t2 and then call join on t2 and starting t1. When t2. join is called, I expect thread t1 to wait but its running in parallel.

Why do you think t1 will be witing for t2 after the call: t2.join()?

The thread that issues "t2.join()" will be waiting. Here, this thread is NOT t1. Which thread is waiting here? t2, t3, or ...?

Greetings,
Piet
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!