• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sleep()

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, don't sleep, here is one question:
Given the code below


public class ThreadDemo extends Thread[
public void run(){
System.out.println("Thread started");
try{
Thread.sleep(20000); //20 seconds sleep
System.out.println("I am Back from my sleep");
}
catch(InterruptedException e){
System.out.println("I got Interrupted");
}
}
}
Which of the following statements are true?
A.Exactly 20 seconds after the start method
is called "I am Back from my sleep" be printed.
B.We can be sure that atleast after 20 seconds elapsed,
"I am Back from sleep" will be printed.
C.The delay between output "Thread started" and "I am Back
from sleep" is less or more than 20 seconds.
D.None of the above.


The answer given is B. But if this thread is interrupted by other thread or by system, can we still be sure that after 20 seconds "I am be Back from sleep" will be printed? Can someone explain to me?
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the difference between A and B. B states that it will be AT LEAST AFTER 20 seconds, will the statement get printed. Even with interruptions from other Threads, eventually "this" Thread will regain the cpu, and then adding whatever amount of time waiting for the cpu to the specified sleep time 20 seconds, we can be sure that it will be at least 20 seconds past that the statement will get to run.
 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But there is no while loop here?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you are referring to the thread being interrupted by a specific invocation of Interrupt() method for an object of ThreadDemo class. I believe in this case "I got Interrupted" would be printed. So can we really be sure that "I am back from sleep" will be printed?

Originally posted by Jason Li:
But there is no while loop here?


 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gidday!
There probably will be a compiler error if you run this code, since there is no main method...
If we can be sure that only one thread is running, I think the answer should be B. But as Anshul points out, as it is we don't know if the thread can be interrupted by another thread, so the correct answer could just as well be D.
/Kaspar
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops... Runtime error, I mean...
//Kas par
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I think that B is the only correct answer.
That's because probably the question is trying to test about sleep() and its known effects.
We have not to think too about "if there's another thread..." or
something else; I think that if there's a portion of code then the questions are about only that code!
It's very interesting thinking about others situations, but it could be confusing for who is studying for the Exam. Don't you think?
Anyway, I love this forum and all you! I hope I can be useful for someone.
Thank you,
Eddy
reply
    Bookmark Topic Watch Topic
  • New Topic