interrupt()并不会中断线程
当有道词典把interrupt翻译过来是中断的意思后,很多人就以为interrupt()是中断线程的意思。如果凭语义去认识这个方法,那估计你也会和我一样,浪费了好久的时间在钻牛角尖上。
先来看一段代码:
package com.adi.waitNInterrupt.demo01;
public class WaitInterupt {
public static void main(String[] args) {
Object o = new Object();
WaitThread wt = new WaitThread(o);
wt.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wt.interrupt();
System.out.println("interrupt~~~");
}
}
class WaitThread extends Thread {
Object o = null;
public WaitThread(Object o) {
this.o = o;
}
@Override
public void run() {
System.out.println("Thread is start !!");
int time = 5;
synchronized (o) {
try {
o.wait();