死锁

来源:互联网 发布:凡科 知乎 编辑:程序博客网 时间:2024/06/10 14:35
class Test2 implements Runnable {    private boolean flag;    Test2(boolean flag) {        this.flag = flag;    }    public void run() {        if (flag) {            while (true) {                synchronized (MyLock.locka) {                    System.out.println(Thread.currentThread().getName()                            + "...if locka ");                    synchronized (MyLock.lockb) {                        System.out.println(Thread.currentThread().getName()                                + "..if lockb");                    }                }            }        } else {            while (true) {                synchronized (MyLock.lockb) {                    System.out.println(Thread.currentThread().getName()                            + "..else lockb");                    synchronized (MyLock.locka) {                        System.out.println(Thread.currentThread().getName()                                + ".....else locka");                    }                }            }        }    }}class MyLock {    static Object locka = new Object();    static Object lockb = new Object();}public class DeadLockTest {    public static void main(String[] args) {        Thread t1 = new Thread(new Test2(true));        Thread t2 = new Thread(new Test2(false));        t1.start();        t2.start();    }}
0 0
原创粉丝点击