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

A simple Thread related Question

 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are asked for one answer and a guarantee then yes the correct answer is "none of the above".
 
Mahesh Chitale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok....... thanks Ankit and Deepak.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ok.

Deepak , is the link (SCJP 5 Mock exam (Word document ) ) from Inquisition
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic