您的位置:首页 > 编程语言 > Java开发

Java--interrupt(),interrupted(),isInterrupted()

2017-06-14 11:10 501 查看

interrupt()

public void interrupt()

interrupt()方法用于线程中断,调用该方法的线程状态被置为“中断”状态。

注意:线程中断仅仅是置线程的中断状态位,不会停止线程。需要用户自己去监视线程的状态为并做处理。支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为“中断状态”,就会抛出中断异常。

代码示例:

public class SimpleThreads {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
long patience = 1000 * 5;

threadMessage("Starting MessageLoop thread");
long startTime = System.currentTimeMillis();
Thread t = new Thread(new MessageLoop());
t.start();

threadMessage("Waiting for MessageLoop thread to finish");
while (t.isAlive()) {
threadMessage("Still waiting...");
// Wait maximum of 1 second
// for MessageLoop thread
// to finish.
t.join(1000);
if (((System.currentTimeMillis() - startTime) > patience)
&& t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
// Shouldn't be long now
// -- wait indefinitely
t.join();
}
}
threadMessage("Finally!");
}
static void threadMessage(String message){
String threadName = Thread.currentThread().getName();
System.out.format("%s: %s%n", threadName, message);
}
private static class MessageLoop implements Runnable {
public void run() {
String importantInfo[] = {
"Mares eat oats",
"Does eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
for (int i = 0; i < importantInfo.length; i++) {
// Pause for 4 seconds
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
threadMessage("I wasn't done!");
threadMessage(Thread.currentThread().isInterrupted()+"");
}
// Print a message
threadMessage(importantInfo[i]);
}
}
}
}


运行结果

main: Starting MessageLoop thread
main: Waiting for MessageLoop thread to finish
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Mares eat oats
main: Still waiting...
main: Tired of waiting!
Thread-0: I wasn't done!
Thread-0: false
Thread-0: Does eat oats
Thread-0: Little lambs eat ivy
Thread-0: A kid will eat ivy too
main: Finally!


interrupted()

public static boolean interrupted()

测试当前线程是否已经中断。线程的中断状态 由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。

方法实现

public static boolean interrupted() {
return currentThread().isInterrupted(true);
}


isInterrupted()

public boolean isInterrupted()

测试线程是否已经中断。线程的中断状态 不受该方法的影响。

方法实现

public boolean isInterrupted() {
return isInterrupted(false);
}


interrupted和isInterrupted主要区别

interrupted(静态方法)是作用于当前正在运行的线程,Thread.interrupted();isInterrupted是作用于调用某一线程,如线程t,t.isInterrupted()表示将线程t的中断位置为中断。

这两个方法都会调用同一个函数,只不过参数一个是true,一个是false

该区别主要体现在方法参数上

private native boolean isInterrupted(boolean ClearInterrupted);


如果这个参数为true,说明返回线程的状态位后,要清掉原来的状态位(恢复成原来情况)。这个参数为false,就是直接返回线程的状态位。

interrupt()和interrupted()比较

interrupt()是用来设置中断状态的。没有返回值,调用该方法后,线程被置为中断位,此时调用isInterrupted()返回为true,当线程中捕获到中断位异常后,线程中的中断状态会被自动清除,此时再调用isInterrupted()返回就是false了,示例见上面代码。

interrupted是静态方法,返回的是当前线程的中断状态。例如,如果当前线程被中断(没有抛出中断异常,否则中断状态就会被清除),你调用interrupted方法,第一次会返回true。然后,当前线程的中断状态被方法内部清除了。第二次调用时就会返回false。如果你刚开始一直调用isInterrupted,则会一直返回true,除非中间线程的中断状态被其他操作清除了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多线程 interrupt