This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tharead start() /join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tharead start() /join()" Watch "Tharead start() /join()" New topic
Author

Tharead start() /join()

Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91
Which of the following outputs are predictable in all platforms?
Mark
Please select two correct answers.


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();


I think output is A & B but true is A & C what is the reason...


Nilesh Patel
SCJP 1.5 - 87%
Justin Yao
Greenhorn

Joined: Jun 16, 2006
Posts: 19
Hi,all
But I always think we start a thread only by calling the start method, never the run method.
Aiglee Castillo
Greenhorn

Joined: Aug 10, 2006
Posts: 27
Yao, this is what I found in the API Specification:

run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.


But I think that A & C are the correct answers because in A you start the first thread, wait for it to finish and then you start the second one and wait for it to finish, so it will always have that behavior.

In B, you start the 2 threads and then wait for the first one to finish, so the 2 threads are running concurrently and you can't predict the behavior.

In C, when you try to run the threads with run() nothing happens, and when you try to join, then nothing happens because the threads are not running, so it is predictable again.

And in D, you try to join the threads that are not running so nothing happens and when you run them, again the behavior is unpredictable.

Am I right?
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
you are absolutely right !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tharead start() /join()