死锁

    xiaoxiao2023-10-08  161

    线程的死锁

    public class DeadLockTest { public static void main(String args[]) { Ytest y = new Ytest(true); Ytest x = new Ytest(false ); Thread t = new Thread(y); Thread t1 = new Thread(x); t.start(); t1.start(); } } class Ytest implements Runnable { private boolean flag; Ytest(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{ public static final Object locka = new Object(); public static final Object lockb = new Object(); }
    最新回复(0)