• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question exam lab. thread, sleep

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer should be 'cannot predict' since s="XYZ"; may indeed run first.
 
Kedar Sarmalkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exactly.....
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i guess using join() we can make the threads do what we want....
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran it .It shows ABC only.I dont understand what's the problem is?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21015
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic