static class MyRun implements Runnable {
@Override
public void run() {
System.out.println("MyRun 当前线程池:" + Thread.currentThread().hashCode());
Thread.currentThread().interrupt();
System.out.println("MyRun 当前线程池中断状态:" + Thread.currentThread().isInterrupted());
}
}
static class MyRun2 implements Runnable {
@Override
public void run() {
System.out.println("MyRun2 当前线程池:" + Thread.currentThread().hashCode());
System.out.println("MyRun2 当前线程池中断状态:" + Thread.currentThread().isInterrupted());
}
}
public static void main(String[] args) {
ExecutorService ex = Executors.newSingleThreadExecutor();
ex.execute(new MyRun());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException();
}
ex.execute(new MyRun2());
}
如上述代码,输出结果如下,为何 MyRun2 中读取的中断状态是 false 呢
MyRun 当前线程池:352001653
MyRun 当前线程池中断状态:true
MyRun2 当前线程池:352001653
MyRun2 当前线程池中断状态:false