Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InterruptedException

 
Praveen Ponna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the java doc "java.lang.InterruptedException" is defined as:


Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.


When I was checking for its usage I found it in join() and sleep() method of thread class and wait() method of Object class.

Why not Yield() method of thread?

Thanks,
Praveen
 
santhosh sharma
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
join( ) , wait( ) and sleep( ) send a thread to a waiting state and not a runnable state
whereas yield( ) sends a thread to a runnable state and not in
a waiting state

when you call the interrupt method on that thread , an InterruptedException
is thrown if and only if the thread is not in a runnable state

that is why methods such as join( ) , wait( ) and sleep( ) throw
an InterruptedException

On the other hand when interrupt is called on a thread which
is running or in a runnable state its "interrupt status" is set
(which can be verified using the isInterrupted method in the Thread class)

Setting the "interrupt status" does not affect the state of the
thread in any way and it contines to be in running or runnable state

Hope i hav cleared u r doubt ,

santhosh sharma (scjp 1.4 100%)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic