Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tharead start() /join()

 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Justin Yao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,all
But I always think we start a thread only by calling the start method, never the run method.
 
Aiglee Castillo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are absolutely right !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic