• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Confused from K&B Book please explain?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain join method of Thread?
K&B Book say
"When one thread calls the join() method of another thread, the currently running thread will wait until the thread it joins with has completed. Think of the join() method as saying, �Hey thread, I want to join on to the end of you. Let me know when you�re done, so I can enter the Runnable state.�
Lets see there are three Thread t1, t2,and t3.
Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();

t1.start();
t2.start();
t3.start();

t1.join();
t3.join();
t2.join();
So in this senerio t2 will run first then t3 then only t1 because K&B say"When one thread calls the join() method of another thread, the currently running thread will wait until the thread it joins with has completed"
[ June 17, 2007: Message edited by: Aaron Raja ]
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


t1.join();
t3.join();
t2.join();
So in this senerio t2 will run first then t3 then only t1 because K&B say"When one thread calls the join() method of another thread, the currently running thread will wait until the thread it joins with has completed"



How did you draw this conclusion?

First, you need to realize that there are 4 threads in this scenario. The first three are the threads that were started, and the fourth is the thread that started the other three. (let's call this the main thread, assuming that this code is in the main method)

So the "t1.join()" command basically tells the main thread to wait until t1 finishes. Once that is done, then it can go on to wait for t3 to finish, and then for t2 to finish.

Also note, that when the main thread waits for t1, t2, or t3 to finish, it may already have been completed, in which case, it will retrun immediately.

You can't draw any conclusion from the code, which thread run first, nor which thread finished first. You only know that the main thread waited for the t1 thread first, t3 thread second, and t2 thread last -- and those threads may not have finished in that order.

Henry
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends !

Then what is guaranteed here, is that MAIN will wait or either way T1 gets chance ? still in discussion or join does not guarantee anything not even if it gets called ? please explain if anybody would

thank you
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chintan,

The only guarantee for Thread.join is that it waits until the thread terminates or there is an InterruptedException. You cannot make any assumptions about the execution of the threads.

If we assume that there is no InterruptedException you have the following guarantees:

t1.join() - After this instruction, t1 has finished. But t2 and t3 may have finished too.

t3.join() - After this instruction, t3 has finished. But t2 may have finished too.

t2.join() - After this instruction, t2 has finished.
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it !

- thanks
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
reply
    Bookmark Topic Watch Topic
  • New Topic