This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
what my doubt is here calling join means that the main thread will have to wait until the Thread T1 has finished which is not started..so i thought that there should be no Output..but it gives End..will it not wait forever??
Thanks & Regards<br /> <br />-Srikanth
M Jairam
Greenhorn
Joined: Nov 04, 2005
Posts: 22
posted
0
1. Note that t1.join(); says that put the current thread on hold until thread t1 has finished. 2. In your example, t1 thread does not even exist - thread only exist after a call to its start() method.
To see join() in action, I have just made slight change to your program. Try it with and without the t1.join();. You will then see how join() works.
Hope this helps.
class TechnoSample { public static void main(String[] args) throws Exception{ Thread t1 = new Thread(getRunnable(20, "t1")); Thread t2 = new Thread(getRunnable(10, "t2")); t1.start(); //t1.join(); t2.start();
System.out.println("End"); } public static Runnable getRunnable(final int id, final String name){ return new Runnable(){ public void run(){ for(int i = 0; i < id; i++){ System.out.print(name+i + " ");
} } }; }}
Kevin Lam
Ranch Hand
Joined: Oct 27, 2005
Posts: 68
posted
0
How about if main() just call join() but not t.join()? what does that mean?
There is no (static) join() method so that won't even compile. You can only join on a thread object (well, also on DatagramSocketImpl, but that's not what is meant here ).
Where does it do that?? It could only do that if the Runnable is also a Thread.
If you try to join on the same thread (like Thread.currentThread.join()) you would get deadlock - it is waiting for itself to end, which it will never do because it is waiting.
There is no black magic happening here. All the join() method does is call the isAlive() method. And if it is alive, it will just call the wait() method. This repeats until the thread is no longer alive.
And BTW, notification is sent by the cleanup code for the thread.