Does anyone can explain to me what does Thread.join() do? The explanation in JDK is too simple. Thanks!
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
It waits for the thread that you're joining to finish.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
It makes a thread stop and wait for the termination of another thread. Let's take a simple example... We have two threads, thread A and thread B. Thread A starts first, which handles a GUI for an application. At some point, the application needs to process a file. Rather than having the GUI freeze up and do nothing but process the file, thread A spawns a new thread, B, to process the file so that thread A is still able to handle user events. Now, let's say that, while thread B is running, the user selects an option to view the results of the file processing. Obviously, it wouldn't be good for thread A to go on until thread B is done processing the file. Therefore, in the code for thread A, you could do this:
That will cause thread A to stop until thread B is done with it's work (its run method completes). I know that's a rather contrived example, but I hope it helps. Corey