• 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

A few questions on wait(), sleep(int) and InterruptedException

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few questions on Threads.

Since sleep(int) only effects the current Thread it is static. But the simular function in Object is not. Shouldn't wait() be static? According to the API it is not. Then can the method wait() cause another thread of execution to wait? That does not seems to be logical since it's purpose is to cause the current thread to wait until another thread invokes the notify() method. Is wait() under the hood calling something like synchronized(this) for the current object it belongs too? Then it would make sense not being static, since it needs 'this'.

Also wait() throws InterruptedException if another thread has interrupted the current thread. But when will this happen? Does it happen when it receives a notify?

And sleep(int) also throws InterruptedException, again how can another thread interrupt the sleep of the current thread? And it is remarkable since another thread cannot cause a Thread to sleep, but it can interrupt its sleep then?
[ August 07, 2007: Message edited by: Marc Wentink ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Sleep and wait have not the same objectives. Sleep allow the current thread, as you said, to stop himself for a certain amount of time.
The wait method is called by a thread from an object instance whose it owns the lock so, wait can't be static.
Yes, the method wait() can cause another thread of execution to wait, as a thread is, after all, an object.

sleep() and wait() will throw an InterruptedException if the sleeping/waiting thread is interrupted by another thread invoking the interrupt method from this thread.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah yes ok, I can see that the Thread class has a method interrupt:



public void interrupt()Interrupts this thread.
First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.

Throws:
SecurityException - if the current thread cannot modify this thread.



This method is not static and you can do something like

Thread theOtherT = new Thread(myRunnable);
theOtherT.start();
// in his own run method theOtherT goes to sleep
theOtherT.interrupt();
// and theOtherT wakes up by the doing in this thread here.

Thank you for the clearance.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by christian combarel:
Yes, the method wait() can cause another thread of execution to wait, as a thread is, after all, an object.

No! wait() causes a wait of the current thread until someone calls notify() or notifyAll() on the object, on that the thread waits.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I saw the above responses and they seem to be correct to me though a bit ambiguous. I am putting in my thoughts below on this. Hope it helps.

Since sleep(int) only effects the current Thread it is static. But the simular function in Object is not. Shouldn't wait() be static?



1.wait causes the current thread to wait and give away the lock it holds. That's why wait is always called in a synchronized block on the locking object of this block. When some other thread using the same locking object calls notifyAll, all other threads waiting on this locking object need to be notified. So here you see that the method wait is very specific to the locking object and hence its a non-static method.

2.sleep causes the current thread to just sleep. No giving away of lock is associated. It just frees the processor to do other things otherwise it can be equated to something like an infinite loop. Once sleep is over same thread can continue processing as it still has the lock. Here we see that method sleep is not associted with any object and hence it is static.


According to the API it is not. Then can the method wait() cause another thread of execution to wait? That does not seems to be logical since it's purpose is to cause the current thread to wait until another thread invokes the notify() method.



I do not quite understand your question. wait always blocks the current thread in which it is called and never blocks any other thread.Technically wait() can be called on any object. But it makes sense to call wait() on a locking object. Because if you call wait on just any other object, first of all it will throw IllegalMonitorStateException if you are not doing this from a synchronized block. If you are doint it inside a synchronized block on a object, the current thread goes to an infinite wait until some other thread notifies on tje locking object.

Also wait() throws InterruptedException if another thread has interrupted the current thread. But when will this happen? Does it happen when it receives a notify? And sleep(int) also throws InterruptedException, again how can another thread interrupt the sleep of the current thread?



wait/sleep throw interrupted exception when another thread calls the interrupt method on the thread contaning the wait/sleep method. One important thing to note is that a thread in wait state gets back the lock once it is interrupted.

Try out this code which is an example of the discussion above:



Hope I am clear. Please post your queries on this thread if you find I am wrong or not clear somewhere.
reply
    Bookmark Topic Watch Topic
  • New Topic