多线程-死锁Test

来源:互联网 发布:中国制裁朝鲜 知乎 编辑:程序博客网 时间:2024/05/16 11:19
class Test implements Runnable{    private boolean flag;    Test(boolean flag)    {        this.flag = flag;    }       public void run()    {        if(flag)        {            while(true)            {                synchronized(Mylock.locka)                {                    System.out.println("if locka");                    synchronized(MyLock.lockb)                    {                        System.out.println("if lockb");                    }                }            }        }        else        {            while(true)            {                synchronized(Mylock.locka)                {                    System.out.println("else lockb");                    synchronized(MyLock.lockb)                    {                        System.out.println("else locka");                    }                }            }        }    }}class Mylock{    static Object locka = new Object();    static Object lockb = new Object();}class  DeadLockTest{    public static void main(String[] args)     {        Thread t1 = new Thread(new Test(true));        Thread t1 = new Thread(new Test(false));        t1.start();        t2.start();    }}
0 0
原创粉丝点击