• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Mock exam thread question

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm using Enthuware and ran across this question.

What will be the output when this program is compiled and run from the command line?



choices are:
It will print "Hello World" twice.
It'll keep printing "Hello World".
2 new threads are created by the program.
1 new thread is created by the program.
None of these.

The correct answer given is None of these.

I chose 1 new thread is created by the program.

The reason I chose this is because I thought they were being tricky and counting main as a thread.
Wouldn't it create one thread for main or would that not be the program from the command line that does that?
 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, depending on the state of the program it can have one, two or even three threads created and only one running.
Another think about it, is that if the program already run (in other words, the main thread is dead), the result will be
hello world
hello world
hello world

and there will be no threads created.

So, I think that "None of these" is the correct answer.

I hope I made myself clear.
 
Ranch Hand
Posts: 63
Firefox Browser Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's true that a there is a tread of execution created so that this program can run. But this program does not create the initial thread; the thread is created before the program even starts to run.
 
Darren Littlepage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it now. So the main method is started by a thread that already existed. The program did not create it. What does create that thread? Is it the JVM when I type 'java Class'?
 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my point of view the right answer would be "none of these". Because thread can only be created using Thread class and here only class that implements Runnable is created and no thread is created to schedule the call to run method.

so when you create instances of the class that implements Runnable and call run method of it , it just executes like normal method call so it will print hello world 3 times and here no option regarding this answer is given. so i guess none of these is right answer.
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur trapasiya wrote:in my point of view the right answer would be "none of these". Because thread can only be created using Thread class and here only class that implements Runnable is created and no thread is created to schedule the call to run method.

so when you create instances of the class that implements Runnable and call run method of it , it just executes like normal method call so it will print hello world 3 times and here no option regarding this answer is given. so i guess none of these is right answer.



I think you really got the point.
The run method was called instead of start.
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic