• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interruption in thread

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have confusion regarding interruption in thread execution.What happened to interrupted thread?Will it give up CPU cycles.?Then, what is interrupted status of thread.?Last one, why interrupted() static function chenges status ?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What happened to interrupted thread?Will it give up CPU cycles.?


Interrupted thread will be put in the ready-to-run state.
Being interrupt() called from a sleeping state it doesn't
use so much CPU cycles.


Then, what is interrupted status of thread.?


Interrupted is not a state but an operation made on a thread
to wake up a thread from sleeping state.


Last one, why interrupted() static function chenges status ?


This is what jdk documentation says
about public static boolean interrupted()
"Tests whether the current thread has been interrupted.
The interrupted status of the thread is cleared by this method.
In other words, if this method were to be called twice in succession,
the second call would return false (unless the current thread were
interrupted again, after the first call had cleared its interrupted
status and before the second call had examined it)."
and about public boolean isInterrupted()
"Tests whether this thread has been interrupted.
The interrupted status of the thread is unaffected by this method.
Returns:true if this thread has been interrupted; false otherwise."
and about 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."
Next have a look at what interrupt() does
Hope that helps.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The interrupt method causes a flag to be set in the thread instance. A thread that is running can ignore the interrupt flag or it can check the interrupt flag. A thread that is sleeping will move into the ready-to-run state when the interrupt method is invoked. The thread will run at the thread schedulers discretion. When it enters the running state it will throw an InterruptedException.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The interruption flag and the interrupt method can be thought as a protocol to enable someone (the interrupt calling thread) to ask for a task (the interrupted thread) to be cancelled. If the run method of the target interruptible thread checks for the interrupted flag before lengthy operations, and/or other convinient points; it can return, cancelling its operation due to the petition of another thread.
If the interrupted thread is waiting, sleeping or joinning, an InterruptedException is thrown. This exception does clear the interruption flag. Thus if you need to propagate the interrupted status, inside an InterruptedException handler it is necessary to call interrupt again.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic