public class SuspendResumeTest {
public static Object u=new Object();
static MyThread thread1=new MyThread("t1");
static MyThread thread2=new MyThread("t2");
public static class MyThread extends Thread{
public MyThread(String name) {
super.setName(name);
}
@Override
public void run() {
System.out.println("in "+getName());
Thread.currentThread().suspend();
}
}
public static void main(String[] args) throws InterruptedException {
thread1.start();
Thread.sleep(1000);
thread2.start();
thread1.resume();
thread2.resume();
thread1.join();
thread2.join();
}
}
我们来分析一波上边的代码
很明显
t1启动后主线程睡了一秒,然后t1就执行了suspend()方法挂起了,一秒后t2启动,t1继续,t2继续,But!!但是t2启动后主线程立即又掉了t2的resume()方法,调用resume方法之前,t2可能还没执行到suspend方法,t2 resume过后才去执行suspend方法,但是主线程已经调用过t2 的resume方法了,不会再有任何线程去调用t2的resume方法,于是它就永远永远挂在那了
通过jstack查看线程详细信息发现确实是这样,没毛病