aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A simple Thread related 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 "A simple Thread related Question" Watch "A simple Thread related Question" New topic
Author

A simple Thread related Question

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

This is a question from CertPal.
In the following program isn't the answer - NONE OF THE ABOVE correct.
" Yes" will be printed at last due to execution of join() but which one among "0 1 2 3 4"
and "Hello" is printed is not guaranteed.Just confirming the answer.





Choose any one option : -
Options

1.) 0 1 2 3 4 Yes Hello

2.) Hello 0 1 2 3 4 Yes

3.) 0 1 2 3 4 Yes Hello Hello

4.) 0 1 2 3 4 Hello Yes

5.) None of the above
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Yes, the output is not predictable as it can be "Hello 0 1 2 3 4 Yes" or "0 1 2 3 4 Hello Yes" or " 0 1 2 Hello 3 4 Yes" etc...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

If you are asked for one answer and a guarantee then yes the correct answer is "none of the above".


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Mahesh Chitale
Greenhorn

Joined: Jan 18, 2010
Posts: 21
yes you are right the output will always have the yes at last
but theres no way to determine which of hello or 1234 will come first.


SCJP 1.6 98%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I think the answer should be hello 01234 yes.
because if after starting the test thread if main gets control it will print hello and then join the test thread but if test thread gets the control it goes to sleep state for 2 seconds due to which main thread will take control and will join the test thread after printing hello.
either way hello is printed first then test threads for loop executes and prints 01234 and then after its completion main thread will print yes.

Please tell me if I am wrong. This is really bad feeling about getting confused after passing the exam.


SCJP 1.6 96%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Neha, when the Test thread sleeps for 2 seconds, there are no guarantees that the main thread will get a chance to execute and print Hello. So the output cannot be guaranteed to be "hello 01234 yes"...
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

I agree that a decent thread scheduler should give the output that you suggest. Perhaps the timeout there is a little too exaggerated. However in theory the output can still not be guaranteed, which is what the SCJP exam would expect from you.

I will update the timeout when time allows
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
ok....... thanks Ankit and Deepak.
Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


Ok.

Deepak , is the link (SCJP 5 Mock exam (Word document ) ) from Inquisition
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Simran Dass wrote:

Ok.

Deepak , is the link (SCJP 5 Mock exam (Word document ) ) from Inquisition


The short answer is yes

That is a word document that users can access in case the java web start on Inquisition fails. It does not contain all the exams from inquisition.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple Thread related Question