方式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....");
            }
        }
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-22
  • 2020-06-17
  • 2022-12-23
猜你喜欢
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-05-03
  • 2021-08-02
  • 2021-08-15
  • 2022-12-23
相关资源
相似解决方案