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
posted
0
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
posted
0
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 ]
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: 128
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.