This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread calling sleep but not blocking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread calling sleep but not blocking" Watch "thread calling sleep but not blocking" New topic
Author

thread calling sleep but not blocking

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Some people say a thread is guaranteed to leave the running state when it calls sleep, wait or join.
This is an experiment to find out whether a thread will exit the running state if it is interrupted before it calls sleep.

The results suggest that Thread-1 never blocks, because main does not take over when Thread-1 calls sleep. (The order of the print statements might not reflect the order of events.)
What do you think? Is the thread blocked by sleep in this case?
[ August 07, 2003: Message edited by: Marlene Miller ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene
I guess that the thread does exit the running state. The output shows that the catch is executed that means the InetrruptedException(or subclass) were thrown. Which will happen when the thread's sleep is disturbed.
Had the the thread been interrupted before entering the sleep() method then there would'nt had been any exception. So I guess that the thread does exit the running state.
I have changed the loop to run for only 10 times

[ August 07, 2003: Message edited by: Anupam Sinha ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Anupam. Your response has made me think more and dig deeper.
I added a test to make sure the interrupted status is set before the thread calls sleep.

I assume sleep behaves like wait with respect to the interruption status. If the interruption status is set *before* a thread calls wait, when the thread executes the wait, an InterruptionException is thrown and the interruption status is set to false. In this case, the thread is not added to the wait set of the object.
So I am saying an InterruptionException can be thrown even when a thread is not currently sleeping or waiting, as long as the interruption status is set.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene
Yes maybe you are correct. I think that, what you are saying is correct.
Cody Beth
Greenhorn

Joined: May 20, 2002
Posts: 11
Hi friends,
In the code below I removed the sleep() method .. and replaced it with an infinte loop in the run method.. With this Thread object is neither sleeping nor waiting ..
With this the InterruptedException is never thrown ..
And the compiler itself complains if the catch as argument as InterruptedException
The modified code is :

Explanation :In the Java Documentation of Thread its mentioned that InterruptedException will be invoked only when wait(), wait(long), or wait(long, int), join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods are invoked
For details refer to :
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Thread.html
----------------------------------------------------------------------------
Now taking the actual example as it is ..and calling the t.interrupt() before t.start()
The InterruptedException is never thrown.

I guess the interrupt flag of the Thread object is cleared as t.start() method is called. And hence no InterruptedException thrown.
I am not sure about this.
Regards,
Cody
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Cody
I guess the interrupt flag of the Thread object is cleared as t.start() method is called. And hence no InterruptedException thrown.
I am not sure about this.

Before t.start() is called there is no thread t.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Anupam for looking at my examples. I want to show you where I learned about how InterruptedException works with wait.
Here is a very precise description of wait and notify. It is a proposed revision of JLS 17.14, Wait Sets and Notification, Doug Lea
[ August 08, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Cody for showing me your experiments.
1. Regarding your first example, if you change the catch clause InterruptedException to Exception then you will not get a compile-time error.
If the main thread interrupts Thread-1 and Thread-1 does not call sleep but catches Exception, the result is.
main
main
main
before sleep true
finally
main
main
main
Therefore, in my example, we can conclude that InterruptedException is be thrown from inside sleep.
2. Regarding your second example, test whether the interruption status has been set.
Thread t = new Test();
t.interrupt(); // interrupt thread before starting it
System.out.println(t.isInterrupted()); // false
t.start();
false // the interruption status is never set
main
before sleep false // therefore, Thread-1 sleeps for 3 seconds
main
main
main
main
<about 3 seconds later>
finally
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread calling sleep but not blocking
 
Similar Threads
KnB notify(), wait(), synchronized block
Question from k&B's masterexam about yield()???????
Queing of several request threads
Q on thread at Jxam
Threads - sleep() & notify()