Loading

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方法,锁未释放;

 

posted @ 2018-01-28 19:53  stono  阅读(204)  评论(0编辑  收藏  举报