wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sleeping state 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 "sleeping state" Watch "sleeping state" New topic
Author

sleeping state

Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Question:
How many seconds will the thread be in the sleeping state when we execute the following method: Thread.sleep(1500)?


1 1.5 seconds
2 approximately 1.5 seconds
3 15 seconds
4 approximately 15 seconds


The answer is 2. I believe what's guaranteed is that the thread will sleep exactly 1.5 seconds and then move to the runnable state contending for the processor. What do you think?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Yap
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I have never read the code for a virtual machine, so I don�t know what happens. My guess is your thread might continue to sleep past the expiration time.
The disk process on your operating system probably has higher priority than the Java virtual machine. There are probably other system processes that also have a higher priority than the Java virtual machine. It could be that your OS views the Java virtual machine as just another application.
If your thread is sleeping and the disk process or some other system process is busy, your thread is going to keep sleeping until that system processes voluntarily gives up the CPU. The system process is not going to yield to some lowly application.
[ October 10, 2003: Message edited by: Marlene Miller ]
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Marlene, here is a quote from Sun's tutorial:

The clockThread in the Clock applet becomes Not Runnable when the run method calls sleep on the current thread:
public void run() {
Thread myThread = Thread.currentThread();
while (clockThread == myThread) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e){
// the VM doesn't want us to sleep anymore,
// so get back to work
}
}
}
During the second that the clockThread is asleep, the thread does not run, even if the processor becomes available. After the second has elapsed, the thread becomes Runnable again and, if the processor becomes available, the thread begins running again.

Sun Tutorial
I don't know if this can be considered trustworty, but here's another piece of authority from API:

sleep
public static void sleep(long millis)
throws InterruptedExceptionCauses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.
Parameters:
millis - the length of time to sleep in milliseconds.
Throws:
InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
See Also:
Object.notify()
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Maybe so. Maybe a hardware interrupt occurs and the interrupt handler moves the thread from a queue of blocked threads to a queue of runnable threads. Then the processor goes back to whatever it was originally doing.
I don�t know how timers are implemented by operating systems.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
After the specified time the thread is again made runeable. The scheduler takes it into account for giving CPU time; that does not mean it is the chosen one to run. Thus the thread will return to acivity after a while that isat least the lapse specified.


SCJP2. Please Indent your code using UBB Code
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Modern Operating Systems, by Andrew Tanenbaum, Chapter 2 Processes and Threads describes Implementing Threads in User Space (2.2.3), Implementing Threads in the Kernel (2.2.4) and Hybrid Implementations (2.2.5). Since there are different ways to implement threads based on the capabilities of the underlying OS, is it possible to make any general statements about how blocking calls are handled?
Then Tanenbaum describes a technique called scheduler activations (2.2.6). When the kernel knows that a thread has blocked, the kernel notifies the process' run-time system, passing as parameters on the stack the number of the thread in question and a description of the event that occurred. The notification happens by having the kernel activate the run-time system at a known starting address, roughly analogous to a signal in UNIX.
"When a hardware interrupt occurs while a user thread is running, the interrupted CPU switches into kernel mode. If the interrupt is caused by an event not of interest to the interrupted process, such as a completion of another process� I/O, when the interrupt handler has finished, it puts the interrupted thread back in the state it was in before the interrupt. If, however, the process is interested in the interrupt, such as the arrival of a page needed by one of the process� threads [or a timer has expired], the interrupted thread is not restarted. Instead, the interrupted thread is suspended and the run-time system started on that virtual CPU, with the state of the interrupted thread on the stack. It is then up to the run-time system to decide which thread to schedule on that CPU: the interrupted one, the newly ready one, or some third choice."
Notice, however all of this applies to the case when the interrupt occurs while the virtual machine has the CPU. But what about the case when the disk process has the CPU? Does the disk process yield to the virtual machine?
[ October 11, 2003: Message edited by: Marlene Miller ]
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Just so we are all nicely confused: I bugged some experienced folks at JVM forum on java.sun.com, and they are positive the sleeping time specified is not mandatory, a thread can actually continue to sleep after its sleeping interval expires. That guy posted over 12000 messages earning 1200+ Duke buks.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thanks Vad. Was it jschell with the 12000 posts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sleeping state
 
Similar Threads
Threads
threads
what is the output
Wait with timeout
threads