首页 > 其他 > 详细

死锁(实现)

时间:2018-06-27 13:12:05      阅读:206      评论:0      收藏:0      [点我收藏+]
public class DeadLock {

    static StringBuffer sb1 = new StringBuffer();
    static StringBuffer sb2 = new StringBuffer();


    public static void main(String[] args) {
        new Thread(()->{
            synchronized(sb1){
                sb1.append("A");
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized(sb2){
                    sb2.append("B");
                    System.out.println(sb1.toString());
                    System.out.println(sb2.toString());
                }
            }
        }).start();

        new Thread(()->{
            synchronized(sb2){
                sb1.append("C");
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized(sb1){
                    sb2.append("D");
                    System.out.println(sb1.toString());
                    System.out.println(sb2.toString());
                }
            }
        }).start();
    }
}

 

死锁(实现)

原文:https://www.cnblogs.com/kaibing/p/9233382.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!