1. 程式人生 > >A 死鎖 の 物語(ˉ▽ˉ;)...

A 死鎖 の 物語(ˉ▽ˉ;)...

/**
 * @author tengshe789
 */
public class DeadLock {
    public static String obj1 = "obj1";
    public static String obj2 = "obj2";
    public static void main(String[] args){
        new Thread(()->{
            try{
                System.out.println("Lock1 running");
                while(true
){ synchronized(DeadLock.obj1){ System.out.println("Lock1 lock obj1"); Thread.sleep(3000);//獲取obj1後先等一會兒,讓Lock2有足夠的時間鎖住obj2 synchronized(DeadLock.obj2){ System.out.println("Lock1 lock obj2"
); } } } }catch(Exception e){ e.printStackTrace(); } }).start();//first thread with lambda new Thread(()->{ try{ System.out.println("Lock2 running"); while
(true){ synchronized(DeadLock.obj2){ System.out.println("Lock2 lock obj2"); Thread.sleep(3000); synchronized(DeadLock.obj1){ System.out.println("Lock2 lock obj1"); } } } }catch(Exception e){ e.printStackTrace(); } }).start();//second thread } }