aspose file tools*
The moose likes Threads and Synchronization and the fly likes relation between interrupt status and sleep method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "relation between interrupt status and sleep method" Watch "relation between interrupt status and sleep method" New topic
Author

relation between interrupt status and sleep method

shankara me
Greenhorn

Joined: Nov 30, 2012
Posts: 14
Hi !
--->My doubt Is on relation ship between sleep method and interrupt status flag.
--->I am asking this doubt based on below two programmes.
--->In programme1, I have called sleep method with out clearing interrupt status .Then InterruptedException was thrown.
--->In programme2 ,I have called sleep method after clearing interrupt status flag using interrupted method.Then InterruptedException was not thrown.
Is there any relation ship between sleep method and interrupt status flag?
Does sleep method throws Interrupted exception based on interrupt status flag?
---------------------------------------------------------------
Programme1:

Output:
10
interrupted
12
interrupted
14
interrupted
16
interrupted
18
interrupted
20
Interrupted
---------------------------------------------------------------
Programme2:

output:
10
12
14
16
18
20
---------------------------------------------------------------

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

shankara me wrote:Hi !
--->My doubt Is on relation ship between sleep method and interrupt status flag.
--->I am asking this doubt based on below two programmes.
--->In programme1, I have called sleep method with out clearing interrupt status .Then InterruptedException was thrown.
--->In programme2 ,I have called sleep method after clearing interrupt status flag using interrupted method.Then InterruptedException was not thrown.
Is there any relation ship between sleep method and interrupt status flag?
Does sleep method throws Interrupted exception based on interrupt status flag?
---------------------------------------------------------------
Programme1:

Output:
10
interrupted
12
interrupted
14
interrupted
16
interrupted
18
interrupted
20
Interrupted
---------------------------------------------------------------
Programme2:

output:
10
12
14
16
18
20
---------------------------------------------------------------




That's how it works. If the thread is interrupted during the sleep() method (or there is an interruption that hasn't been handled yet), then the method will clear the interrupt flag, and throw the InterruptedException.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: relation between interrupt status and sleep method