This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic