File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Interrupt method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Interrupt method" Watch "Thread Interrupt method" New topic

Thread Interrupt method

Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
In the explanation given by Jim
Calling Interrupt on a thread that is,
In running state?
I don't know if the thread will necessarily move to ready state as Ajith says. Threads can move back and forth from running to ready all the time anyway, so this doesn't make too much difference.
No exception is thrown. However the interrupted flag is set, which means that if you call interrupted() or isInterrupted(), they will return true. If the thread later goes from running state to waiting or sleeping state before interrupted flag is cleared via interrupted() method, then an InterruptedException will be thrown and the flag will be cleared.
I understood that there is no effect on calling the interrupt
method in the running status of the thread.
My doubt is,
when exactly is the interrupted exception thrown,
a. before it goes to sleeping state,
b. after going to sleeping state but before ready state,
c. after sleep is over , ready is over and it gets to run.
Please give a clear and sutiable reply.

V. Kishan Kumar

Regards,<BR>V. Kishan Kumar
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Please somebody take care and time to reply for the question
I posted about the interrupt method acting on a running thread.
V. Kishan Kumar
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Some basics -
<code>InterruptedException</code> is thrown if another thread calls <code>interrupt()</code> on the sleeping thread while it is asleep. If <code>sleep()</code> is setting an alarm clock before bed, interrupt() is a midnight phone call.
The exception will also get thrown if the "interrupted" flag is set in the thread, which happens if another thread calls interrupt even before the original thread sleeps.
Calling <code>interrupt()</code> on a thread that is not sleeping does cause the next call to <code>sleep()</code> by the thread to immediately throw an <code>InterruptedException</code>, assuming the thread's interrupted flag has not been cleared.

So, the answer to your questions depends on whether the interrupted flag is set or not.

  • If it is set, then if the thread is currently in the sleeping state, it gets thrown in the earliest immediate future.
  • Otherwise ie., if the thread is currently not sleeping and the interrupted flag is set, the exception gets thrown before the thread goes to sleep the very next time.

  • Hope that clears the confusion

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: Thread Interrupt method
It's not a secret anymore!