• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Tharead start() /join()

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are absolutely right !!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic