Howdy, ranchers!
chintan ramavat posted yesterday
(...) so when you hit the join() method, t1.join(t2) so t2 is the (...)
t1.join(t2) ?
No, you can't do that.
You cannot specify a thread to join a certain thread. There is no method of join() that takes a thread / runnable as argument.
It is only possible to give join() a timeout as parameter.
The
API about join() is indeed a bit laconic. It tells you only six words:
Waits for this thread to die.
Nothing else. But it explains everything.
When you see something like
aThread.join(); it means that the
currentthread (the one who reads the line) will wait until aThread has finished its run method and dies thereafter.
And this is guaranteed, in threading a lot is platform dependent, but this is safe: the current thread will wait.
Demo:
From the output you see that the current thread (main in this case) waits, until thr2 is ready. You may also see things like this:
...
thr1: i=3
mainthr1: i=2
: i=2
...
because there is no synchronization, but that the main thread waits for thr2 in the second part is guaranteed.
Yours,
Bu.