• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

thread calling sleep but not blocking

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene
Yes maybe you are correct. I think that, what you are saying is correct.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic