方式1:早期JAVA采用suspend()、resume()对线程进行阻塞与唤醒,但这种方式产生死锁的风险很大,因为线程被挂起以后不会释放锁,可能与其他线程、主线程产生死锁,如:
public class ThreadSuspendTest { public static void main(String[] args) { Thread mt = new MyThread(); mt.start(); try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } mt.suspend(); System.out.println("suspend complete?"); mt.resume(); } static class MyThread extends Thread { public void run() { while (true) { System.out.println("running...."); } } } }