This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regding Thread.join Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regding Thread.join" Watch "regding Thread.join" New topic
Author

regding Thread.join

swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
another thread doubt from Sun's mock exam

Which of the following outputs are predictable in all platform?
(Select three correct answers)
-----------------------------------------------------------------------
Assume t1, t2 are all newly created valid Thread objects.
Answer is - A,C,D
-----------------------------------------------------------------------
A:
t1.start();
t1.join();
t2.start();
t2.join();
B:
t1.start(); // what's the difference between B and C. If C is valid,WHy not B??
t2.start();
t1.join();
t2.join();
C:
t1.run();
t2.run();
t1.join();
t2.join();
D:
t2.join(); // Can we call join on thread before starting it??
t1.start();
t1.join();
t2.start();

Thanx


Swati Kale
SCJP
SCWCD
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Good question Swathi.
Here is my thought.
When you create and start threads from main(), main will be
parent and the subsequent threads started from main() becomes child threads
In general, parent(main) thread executes first and dies, but the
daemon thread will be alive until the child threads are completely done executing.
Now, how do we make this parent(main) to wait until her child threads are done executing? join() comes handy
Take a look at this code:
public class Vest extends Thread{
String a ;
Vest(String a ) {
this.a = a;
}
public void run() {
for (int i = 0; i<10; i++) {
System.out.println(a + "" + i);
}
}
public static void main(String args[]) {
Vest v = new Vest("First Thread");
Vest v1 = new Vest("Second Thread");
v.start();
v1.start();
try{
System.out.println("wait for the child threads to finish");
v.join();
v1.join();
if(!v.isAlive())
System.out.println("First Thread not alive");
if(!v.isAlive())
System.out.println("Second Thread not alive");
} catch (InterruptedException e ){
System.out.println("Main Thread interruped");
}
System.out.println("Exit from main thread");
}
}
Coming to your Question
I dont think you can join a thread thats not started.
Try commenting out
v.start();
v1.start();
you will know
Let me know swati, if you have any questions
Ragu
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
Hi, swati and Ragu. See the following code:

Thread t1 is joined before started. Thread t2 is joined after started. This code compiles and runs fine.
In my humble opinion, join() method will act just like a normal method over the thread object if the thread is not started. But if the thread is started, it will wait the thread finishing.
Swati, the difference between C and D is that, C starts thread which in turn running the code inside run() method but D only call the run() method in a normal way just as other object method invocation.
It seems we can join a thread before it started, in this case, there is no effect over the thread.
Correct me if i am wrong.
Regards,

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464

Thread t1 is joined before started. Thread t2 is joined after started. This code compiles and runs fine.
In my humble opinion, join() method will act just like a normal method over the thread object if the thread is not started. But if the thread is started, it will wait the thread finishing.
Swati, the difference between C and D is that, C starts thread which in turn running the code inside run() method but D only call the run() method in a normal way just as other object method invocation.
It seems we can join a thread before it started, in this case, there is no effect over the thread.
Correct me if i am wrong.
Regards,
[/b]
</BLOCKQUOTE>
Exactly Sun.
The code will compile and run if we just
join() a thread without starting it
However there is no use to the join(), since the child thread is not started .The main intention/purpose of join() itself is to make the parent thread wait until the child completes execution.

[This message has been edited by Ragu Sivaraman (edited September 08, 2001).]
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
As per my understanding , the correct answer to above q is , options A & C but not B or D
here are my reasons :
For Option A :
Two Threads named t1 n t2 are being started n joined ( in tht order, one by one ) to the main thread ( i assume tht above calls r from main() )
t1.start(); // t1 started , ready to run
t1.join(); // t1 runs , t1 will be allowed to run until it finishes , main ll wait for tht
t2.start(); // t2 started , ready to run
t2.join(); // t2 runs , t2 will be allowed to run until it finishes , main ll wait for tht
So output is predictable , first comes output of Thread t1 , followed by Thread t2
( and of course , before n after , the output of main )

For Option B :
Two Threads named t1 n t2 are being started n joined to the main thread ,
t1.start(); // t1 started , ready to run
t2.start(); // t2 started , ready to run
t1.join(); // t1 gets running , t1 will be allowed to run until it finishes , main ll wait for tht
t2.join(); // main ll wait for t2 to finish ( t2 may already be running ! )
At line3 , when t1 gets running , there is one more Thread which is ready to run , it is t2
Which Thread t1 or t2 'll get to run n for how long , cannot be predicted .
On some platforms the second thread may get to run only after first finsihes or move to wait state ( like on preemptive ) , on others both t1 n t2 may get a time-slice alrenatively to run within ( like on time-slice implementations ) , etc.
So Bottom line is , Output IS NOT predictable on all platform in this case
For Option C :
This option is diff from all the other three , NO THREAD OTHER THAN MAIN ever get started !.
t1.run(); // explicit call to run() of t1
t2.run(); // explicit call to run() of t2
t1.join(); // Call to t1's join() , has no effect as t1 is nt yet started
t2.join(); // Call to t2's join() , has no effect as t2 is nt yet started
At line1 , we are making a explicit call to t1's run() ( No new Thread is spawned , remember calling run() method over Thread , DONOT starts it ) , so it runs n prints its output whtever it is
similarly , At line2 , we are making a explicit call to t2's run() ( No new Thread is spawned )
Line3 n line4 , Does join() main Thread to 2-threads , t1 n t2 , but these threads never get start()ed ! so these hav no effect
( take it this way , just one THread is there , main tht is , so output is predictable becoz of sequential order for given Thread )

For Option D :
t2.join(); // Call to t2's join() , has no effect as t2 is nt yet started
t1.start(); // t1 started , ready to run
t1.join(); // t1 runs , t1 will be allowed to run until it finishes , main ll wait for tht
t2.start(); // t2 started , ready to run
At line4 , when t2 get ready to run , there is one more Thread , which is already running , it is Main
If the priority of t2 is higher than main() , then on preemptive implementations , t2 may get to run immediately ,
or both t2 n main may get a time-slice alternatively to run within on time-slice implementations , etc.
So , which Thread t2 or main 'll get to run n for how long , cannot be predicted
To get my point , plz look at following code :


Output of sample run is :
i am Main , eh?
t1:HO! t1:HO! t1:HO! t1:HO! t1:HO! t1 over !
1#Main t2:HO! 2#Main t2:HO! 3#Main 4#Main t2:HO! main over !
t2:HO! t2:HO! t2 over !

Carefully look at last two lines of output , they are no-way predictable for all systems
On time-slice system , last two lines can be diff
So i think Option D is incorrect. lemme knw ur opinions plz

As for question " Can we call join on thread before starting it?? " :
Well , Given the reference to the object, wht we need to invoke a method over an object ?
Just two things ; reference n method both shld be accessible , right
wht t1.join() is in bare bones? , just tht , i mean a method call given reference t1 .
So of course , we can call join() before a call to start() , but it 'll have no effect

( A misplaced TIP to all ranchers : When i c word "predicable" in threads q's , or word "guaranteed" in garbage coll. q's , i ll just recite this to myself tht " most of the things in Threads are not predictable , as most of the things are not guaranteed in garbage coll... " )

------------------
Gagan (/^_^\)


Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Thanx Raghu,Gagan and Sun for the explanation.May be Sun wld like to verify his question again.
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
Thank you, swati, Gagan. I have modified the question as Gagan explained in order to avoid confusion. Thanks.

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: regding Thread.join
 
Similar Threads
Tharead start() /join()
Threads
How to make one thread die before another thread
How to wait until all threads finishes task?
join() start() run()