Hi everyone, Do we need to know Thread.join() for the 1.4 exam? Thanks. Mansi
Barkat Mardhani
Ranch Hand
Joined: Aug 05, 2002
Posts: 787
posted
0
I am assuming so. I read from the feedback from those who took 1.4 that test included 8-10 heavy-duty thread questions....
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
There's not much to know about join. It blocks waiting for the thread you're joining to finish running.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Timothy Toe
Ranch Hand
Joined: Oct 19, 2002
Posts: 156
posted
0
This program shows the working of the ThreadObject.join() method. The join() method forces a thread to wait for another thread to die before it is allowed to continue executing. Or, put in another way, thread A may call thread B’s join() method if A wants to stop executing and wait for thread B to finish before A continues. Without t.join() – notice that t’s count is after main’s begin and end. This is because both the main and t threads execute independent of each other and since t sleeps, the main thread has plenty of CPU time to finish executing.
With t.join() - The output shows that T’s counting activity is between main’s begin and end. Now main waits for t to die 1st before continuing – because main calls t.join(). By calling this method, main tells the t thread that “I will join after you !”
Some extra experimenting : Call t.join() before calling t.start() – This means main is telling t that it wants to join at the end of t’s execution EVEN BEFORE t STARTED EXECUTING. t.join() will have no effect on main. Maybe t replies : “you may continue since I have not even started executing… do not wait for me.” Join only makes main wait if t is already started.