Anda di halaman 1dari 5

6/12/13

A few questions on wait(), sleep(int) and InterruptedException (SCJP forum at JavaRanch)

Big Moose Saloon


Search | Java FAQ | Recent Topics A friendly Register / Login place for programming greenhorns! A special promo: Enter your blog post or vote on a blogger to be featured

in an upcoming Journal

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

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


posted 8/7/2007 4:36 PM

Marc Wentink Ranch Hand Joined: May 18, 2007 Posts: 142

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 ]
SC JP5

christian combarel Ranch Hand Joined: Aug 04, 2007 Posts: 47

posted 8/8/2007 2:36 AM

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.

www.coderanch.com/t/264358/java-programmer-SCJP/certification/questions-wait-sleep-int-InterruptedException

1/5

6/12/13

A few questions on wait(), sleep(int) and InterruptedException (SCJP forum at JavaRanch)

sleep() and wait() will throw an InterruptedException if the sleeping/waiting thread is interrupted by another thread invoking the interrupt method from this thread.

------------C hris

Marc Wentink Ranch Hand Joined: May 18, 2007 Posts: 142

posted 8/8/2007 2:44 AM

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.

Manfred Klug Ranch Hand Joined: Jun 04, 2007 Posts: 377

posted 8/8/2007 10:50 AM

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.

Satya Maheshwari Ranch Hand Joined: Jan 01, 2007 Posts: 368

posted 8/8/2007 2:55 PM

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 www.coderanch.com/t/264358/java-programmer-SCJP/certification/questions-wait-sleep-int-InterruptedException

2/5

6/12/13

A few questions on wait(), sleep(int) and InterruptedException (SCJP forum at JavaRanch) 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:

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . c l a s sL o c k{ 0 2 . p r i v a t es t a t i cb o o l e a ni n s t a n c e E x i s t s=f a l s e ; 0 3 . p r i v a t es t a t i cL o c ki n s t a n c e=n u l l ; 0 4 . p r i v a t eL o c k ( ){ 0 5 . i n s t a n c e E x i s t s=t r u e ; 0 6 . } ; 0 7 . s t a t i cL o c kg e t I n s t a n c e ( ){ 0 8 . i f ( ! i n s t a n c e E x i s t s ) 0 9 . i n s t a n c e=n e wL o c k ( ) ; 1 0 . r e t u r ni n s t a n c e ; 1 1 . } 1 2 . 1 3 . } 1 4 . 1 5 . 1 6 . c l a s sT h r e a d 1e x t e n d sT h r e a d{ 1 7 . p u b l i cv o i dr u n ( ){ 1 8 . s y n c h r o n i z e d ( L o c k . g e t I n s t a n c e ( ) ){ 1 9 . S y s t e m . o u t . p r i n t l n ( " T h r e a d 1g o tt h el o c k " ) ; 2 0 . S y s t e m . o u t . p r i n t l n ( " T h r e a d 1i ns l e e pn o wb u th a st h el o c k " ) ; 2 1 . t r y{ www.coderanch.com/t/264358/java-programmer-SCJP/certification/questions-wait-sleep-int-InterruptedException

3/5

6/12/13

2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 .

A few questions on wait(), sleep(int) and InterruptedException (SCJP forum at JavaRanch) t r y{ T h r e a d . s l e e p ( 5 0 0 0 ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ne x ){ S y s t e m . o u t . p r i n t l n ( " T h r e a d1w a si n t e r r u p t e d " ) ; e x . p r i n t S t a c k T r a c e ( ) ; } S y s t e m . o u t . p r i n t l n ( " T h r e a d 1a w a k en o wa n dh a st h el o c k " ) ; S y s t e m . o u t . p r i n t l n ( " T h r e a d 1n o t i f y i n go t h e rt h r e a d sw a i t i n go n l o c k " ) ; L o c k . g e t I n s t a n c e ( ) . n o t i f y A l l ( ) ; S y s t e m . o u t . p r i n t l n ( " T h r e a d 1d i e d " ) ; } }

} c l a s sT h r e a d 2e x t e n d sT h r e a d{ p u b l i cv o i dr u n ( ){ s y n c h r o n i z e d ( L o c k . g e t I n s t a n c e ( ) ){ S y s t e m . o u t . p r i n t l n ( " T h r e a d 2g o tt h el o c k " ) ; S y s t e m . o u t . p r i n t l n ( " T h r e a d 2g i v i n gu pl o c ka n dg o i n gt ow a i t " ) ; t r y{ L o c k . g e t I n s t a n c e ( ) . w a i t ( ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ne x ){ S y s t e m . o u t . p r i n t l n ( " T h r e a d2w a si n t e r r u p t e d " ) ; e x . p r i n t S t a c k T r a c e ( ) ; } S y s t e m . o u t . p r i n t l n ( " T h r e a d 2w a i ti so v e ra si tg o tt h el o c kb a c k " ) ; S y s t e m . o u t . p r i n t l n ( " T h r e a d 2d i e d " ) ; } } } c l a s sM a i n{ p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ){ T h r e a d 1t 1=n e wT h r e a d 1 ( ) ; T h r e a d 2t 2=n e wT h r e a d 2 ( ) ; t 1 . s t a r t ( ) ; t 2 . s t a r t ( ) ; t 2 . i n t e r r u p t ( ) ; } }

Hope I am clear. Please post your queries on this thread if you find I am wrong or not clear somewhere.

Thanks and Regards

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

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

Similar Threads Threads Notes for Exams


www.coderanch.com/t/264358/java-programmer-SCJP/certification/questions-wait-sleep-int-InterruptedException 4/5

6/12/13

A few questions on wait(), sleep(int) and InterruptedException (SCJP forum at JavaRanch)

interrupt() interrupt Thread INTERRUPTEDEXCEPTION


All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 06:32:14 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/264358/java-programmer-SCJP/certification/questions-wait-sleep-int-InterruptedException

5/5

Anda mungkin juga menyukai