File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question exam lab. thread, sleep Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question exam lab. thread, sleep" Watch "question exam lab. thread, sleep" New topic
Author

question exam lab. thread, sleep

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Hi!

source: http://www.examlab.tk/

The question is:


The answer is: Prints "ABC"
I agree that maybe 100% of the times it will print ABC, but I think we can't strictly say this.
I think the correct answer should be: Result is unpredictable.

Help me! What am I missing?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
well Thread.sleep(1000) will slow down the main thread not the thread you started.....does this help you...becasue i didnt understand anything
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The answer should be 'cannot predict' since s="XYZ"; may indeed run first.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Kedar Sarmalkar
Greenhorn

Joined: May 25, 2007
Posts: 16
Deepak Bala wrote:The answer should be 'cannot predict' since s="XYZ"; may indeed run first.


Right.... with thread its always unpredictable.... It totally depends on the implementation of thread scheduler in the JVM.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
exactly.....
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
but i guess using join() we can make the threads do what we want....
jayalakshmi charugundla
Ranch Hand

Joined: Jul 22, 2009
Posts: 57
I ran it .It shows ABC only.I dont understand what's the problem is?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

jayalakshmi charugundla wrote:I ran it .It shows ABC only.I dont understand what's the problem is?


The problem is that you cannot rely on the started thread to run before Thread.sleep(). It might well be that Thread.sleep() runs, sets the string to XYZ and then the other thread executes after this. So the output could be ABC or XYZ.
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Deepak Bala wrote:The answer should be 'cannot predict' since s="XYZ"; may indeed run first.

Thanks Deepak! So my answer was right. Yeah. =)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Leandro Coutinho wrote:
Thanks Deepak! So my answer was right. Yeah. =)


Yes, but it will take one seriously overloaded (or broken) system, that will not give the newly create thread a timeslice for a whole second, while the main thread is asleep (and no other thread is runnable).

There is a race condition, so technically, it is unpredictable -- but I would easily bet money on the outcome for this.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

but I would easily bet money on the outcome for this.


I have to agree with Henry here. Although the nitpick answer for this question, according to the SCJP, is unpredictability, in real life scenarios you would expect a decent scheduler to allow the answer ABC to appear most, if not all times.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question exam lab. thread, sleep
 
Similar Threads
Thread Question - Examlab Practice Exam 1
Thread outuput predictable or not
Deadlock
Thread's question
question about thread output from examlab practice test 1