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

Thread Question

 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain how the following program comes up with it's answer. I've left off the answer to see if you can guess what it is supposed to be before you compile, run & respond.
 
Ratna Singh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
I believe the program with print out main, val 1, val 2 in any order depending on if the start() returns and the main() completes execution before the two new threads can execute run().
I am now going to run and check this
Ratna
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the output cannot be determined because start() returns immediately after the invocation and delegates the job of starting and scheduling the thread to the scheduler.
Even if the first ThreadTest is constructed (and started) before the second one, it doesn't mean it will run first.
Ratna, it is possible that both newly created threads run before the main thread ends. There is no guarantees as to which thread runs since they all have the same priority and the scheduling is platform-dependent.
And the test answer is wrong I'm afraid !!!
[ March 18, 2002: Message edited by: Valentin Crettaz ]
 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ratna,

the program with print out main, val 1, val 2 in any order

That's what I guessed when looking at the code. Per the quiz, the answer was main, val 1, val 2 in that order. Which is why I'm posting this question.
[ March 18, 2002: Message edited by: Mike Cunningham ]
 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it with a for loop. Which validates what both of you just pointed out. Thanks.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike please indicate which mock this question comes from, after what I'll move this thread to Mock Exam errata. Thanks
[ March 18, 2002: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic