码迷,mamicode.com
首页 > 其他好文 > 详细

死锁示例

时间:2017-11-18 17:35:52      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:thread   style   sync   rgs   sys   ring   start   static   art   

class Test implements Runnable{
    private boolean flag;
    Test(boolean flag){
        this.flag = flag;
    }
    public void run(){
        if(flag){
            synchronized(Mylock.locka){
                System.out.println("if..locka..");
                synchronized(Mylock.lockb){
                    System.out.println("if..lockb..");
                }
            }
        }
        else{
            synchronized(Mylock.lockb){
                System.out.println("else...lockb..");
                synchronized(Mylock.locka){
                    System.out.println("else...locka..");
                }
            }
        }        
    }    
}
class Mylock{
    public static final Object locka = new Object();
    public static final Object lockb = new Object();
}
class LockTest{
    public static void main(String[] args){
        Test x1 = new Test(true);
        Test x2 = new Test(false);
        Thread t1 = new Thread(x1);
        Thread t2 = new Thread(x2);
        t1.start();
        t2.start();
    }
}

 

死锁示例

标签:thread   style   sync   rgs   sys   ring   start   static   art   

原文地址:http://www.cnblogs.com/guoyunhao/p/7857011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!