aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
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

Joined: Mar 17, 2002
Posts: 19
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
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

Joined: Nov 14, 2000
Posts: 129
I tried it with a for loop. Which validates what both of you just pointed out. Thanks.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question