标签:throw sleep starting exception als exiting skin 检测 处理
public static boolean interrupted () { return currentThread().isInterrupted(true); }
public boolean isInterrupted () { return isInterrupted( false); }
private native boolean isInterrupted( boolean ClearInterrupted);
如果这个参数为true,说明返回线程的状态位后,要清掉原来的状态位(恢复成原来情况)。
这个参数为false,就是直接返回线程的状态位。
class Example2 extends Thread { public static void main(String args[]) throws Exception { Example2 thread = new Example2(); System.out.println("Starting thread..."); thread.start(); Thread.sleep(3000); System.out.println("Asking thread to stop..."); // 发出中断请求 thread.interrupt(); Thread.sleep(3000); System.out.println("Stopping application..."); } public void run() { // 每隔一秒检测是否设置了中断标示 while (!Thread.currentThread().isInterrupted()) { System.out.println("Thread is running..."); long time = System.currentTimeMillis(); // 使用while循环模拟 sleep while ((System.currentTimeMillis() - time < 1000) ) { } } System.out.println("Thread exiting under request..."); } }
interrupt、interrupted和isInterrupted的区别
标签:throw sleep starting exception als exiting skin 检测 处理
原文地址:http://www.cnblogs.com/lnas01/p/7896169.html