It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interruption in thread 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 "interruption in thread" Watch "interruption in thread" New topic

interruption in thread

Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
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 ?

--------------<br />Prashant<br />SCJP-91%
Igor Zeta
Ranch Hand

Joined: Nov 12, 2002
Posts: 40
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.

ciao<br />Igor Zeta<br />SCJP1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: interruption in thread
It's not a secret anymore!