Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Interrupt method

 
Kishan Kumar
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.

------------------
Regards,
V. Kishan Kumar
 
Kishan Kumar
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please somebody take care and time to reply for the question
I posted about the interrupt method acting on a running thread.
------------------
Regards,
V. Kishan Kumar
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Ajith
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic