线程中比较容易搞混淆的几个方法:
isInterrupted:
public boolean isInterrupted() {
return isInterrupted(false);
}
interrupted:
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
interrupt:
public void interrupt() ;
这三个方法都是关于线程状态的方法,线程的初始状态返回的应该是false;
interrupt方法使用于修改线程状态,此时线程的状态被修改为true
如果此时调用isInterrupted会返回true,调用静态方法interrupted也会返回true,但是
interrupted是会重置线程状态,贴代码说明:
Thread current = Thread.currentThread();
current.interrupt();
System.out.println(Thread.interrupted());
System.out.println(current.isInterrupted());
此时控制台输出:true,false;
如果改变代码顺序:
Thread current = Thread.currentThread();
current.interrupt();
System.out.println(current.isInterrupted());
System.out.println(Thread.interrupted());
此时返回的是:
true,true