Java Thread Interrupt && API Introduction
2009-03-08 11:03
274 查看
http://java.sun.com/docs/books/tutorial/essential/concurrency/interrupt.html
Interrupts
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. This is the usage emphasized in this lesson.
A thread sends an interrupt by invoking
Many methods that throw
What if a thread goes a long time without invoking a method that throws
In this simple example, the code simply tests for the interrupt and exits the thread if one has been received. In more complex applications, it might make more sense to throw an
This allows interrupt handling code to be centralized in a
By convention, any method that exits by throwing an
API Introduction:
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the
If this thread is blocked in an invocation of the
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a
If this thread is blocked in a
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
Throws:
Interrupts
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. This is the usage emphasized in this lesson.
A thread sends an interrupt by invoking
interrupton the
Threadobject for the thread to be interrupted. For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.
Supporting Interruption
How does a thread support its own interruption? This depends on what it's currently doing. If the thread is frequently invoking methods that throwInterruptedException, it simply returns from the
runmethod after it catches that exception. For example, suppose the central message loop in the
SleepMessagesexample were in the
runmethod of a thread's
Runnableobject. Then it might be modified as follows to support interrupts:
for (int i = 0; i < importantInfo.length; i++) { //Pause for 4 seconds try { Thread.sleep(4000); } catch (InterruptedException e) { //We've been interrupted: no more messages. return; } //Print a message System.out.println(importantInfo[i]); }
Many methods that throw
InterruptedException, such as
sleep, are designed to cancel their current operation and return immediately when an interrupt is received.
What if a thread goes a long time without invoking a method that throws
InterruptedException? Then it must periodically invoke
Thread.interrupted, which returns
trueif an interrupt has been received. For example:
for (int i = 0; i < inputs.length; i++) { heavyCrunch(inputs[i]); if (Thread.interrupted()) { //We've been interrupted: no more crunching. return; } }
In this simple example, the code simply tests for the interrupt and exits the thread if one has been received. In more complex applications, it might make more sense to throw an
InterruptedException:
if (Thread.interrupted()) { throw new InterruptedException(); }
This allows interrupt handling code to be centralized in a
catchclause.
The Interrupt Status Flag
The interrupt mechanism is implemented using an internal flag known as the interrupt status. InvokingThread.interruptsets this flag. When a thread checks for an interrupt by invoking the static method
Thread.interrupted, interrupt status is cleared. The non-static
Thread.isInterrupted, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.
By convention, any method that exits by throwing an
InterruptedExceptionclears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking
interrupt.
API Introduction:
interrupt
public void interrupt()
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the
checkAccessmethod of this thread is invoked, which may cause a
SecurityExceptionto be thrown.
If this thread is blocked in an invocation of the
wait(),
wait(long), or
wait(long, int)methods of the
Objectclass, or of the
join(),
join(long),
join(long, int),
sleep(long), or
sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an
InterruptedException.
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a
ClosedByInterruptException.
If this thread is blocked in a
Selectorthen the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's
wakeupmethod were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
Throws:
SecurityException- if the current thread cannot modify this thread
相关文章推荐
- java线程基础巩固---Thread中断Interrupt方法学习&采用优雅的方式结束线程生命周期
- 解决Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/ FSDataInputStream
- Exception in thread "main" java.lang.NoClassDefFoundError: org.jaxen.NamespaceContext
- Exception in thread "main" java.lang.LinkageError错误的解决
- Exception in thread "main" java.io.IOException: Invalid header signature; read 1688935826934608, exp
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/Lin
- Exception in thread "main" java.lang.NoClassDefFoundError: Demo (wrong name: com/zhangyun/Demo)解决办法
- Exception in thread "main" java.lang.NoClassDefFoundError
- Eclipse迅速执行:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- Java Thread.interrupt 害人中断JAVA线程
- Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]" java.lang.OutOfMemoryEr
- Exception in thread "main" java.lang.NoSuchFieldError: level
- JAVA ThreadPool & ThreadFactory
- Exception in thread "main" java.lang.IllegalArgumentException
- Eclipse下好使,但是cmd下执行java运行class文件提示Exception in thread "main" java.lang.NoClassDefFoundError问题
- Java Thread&Concurrency(11): 深入理解ThreadPoolExecutor及其实现原理
- 求助 Exception in thread "main" java.lang.NoClassDefFoundError:
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 解决方案
- Java Thread.interrupt方法
- 浅谈Java线程Thread之interrupt中断解析