aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
Hi

Assume t1, t2 are all newly created valid Thread objects.
-------------------------------------------------------

A.
t1.start();
t1.join();
t2.start();
t2.join();

B.
t1.start();
t2.start();
t1.join();
t2.join();

C.
t1.run();
t2.run();
t1.join();
t2.join();

D.
t1.join();
t2.join();
t1.start();
t2.start();

Which of the following outputs are predictable in all platforms?
The given answer is A & C
Why not B???
Plz Explain me....


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
The reason B is incorrect is that t1 and t2 could be executing loops that print out messages at the same time, and there is no way to predict exactly what order the messages will be in. For example, the output could be alternating like this:

This is message 1 from t1
This is message 1 from t2
This is message 2 from t1
This is message 2 from t2
This is message 3 from t1
This is message 3 from t2

or, the output could show t1 finishing before t2 starts, like this:

This is message 1 from t1
This is message 2 from t1
This is message 3 from t1
This is message 1 from t2
This is message 2 from t2
This is message 3 from t2

There is no way to predict the order of execution of the statements in t1 and t2 when they are both running at the same time, as would be the case in choice B.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
A is correct, because t1 completes before t2 starts, C is correct because no new threads are created. The run() method doesn't create new threads, only the start() method does that.
 
wood burning stoves
 
subject: Threads