Thread.suspend和println使线程死锁

Thread.suspend和println使线程死锁

package com.stono.thread2.page39;

public class MyThread extends Thread{
    private long i = 0;
    @Override
    public void run() {
        while(true){
            i++;
            System.out.println(i);
        }
    }
    
    public static void main(String[] args) {
        try {
            MyThread thread = new MyThread();
            thread.start();
            Thread.sleep(1000);
            thread.suspend();
            System.out.println("main end!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

因为println的源码:

    public void println(long x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

println的时候,调用了suspend方法,锁未释放;

原文地址:https://www.cnblogs.com/stono/p/8372211.html