With regard to the problem of multithreaded deadlocks, I want to write a deadlock program. One of my two programs can lock successfully, but the other won't.

the first code can be locked successfully

public class Main {

public static void main(String[] args){
    LockThread lock1 = new LockThread(true);
    LockThread lock2 = new LockThread(false);
    
    Thread thread1 = new Thread(lock1);
    Thread thread2 = new Thread(lock2);
    
    thread1.start();
    thread2.start();
}

}

class LockThread implements Runnable {

private boolean flag;

public LockThread(boolean flag){
    this.flag = flag;
}

public void run(){
    if (flag){
        synchronized(MyLock.lock1){
            System.out.println("Lock1");
            synchronized(MyLock.lock2){
                System.out.println("Lock2");
            }
        }
    }else{
        synchronized(MyLock.lock2){
            System.out.println("Lock2");
            synchronized(MyLock.lock1){
                System.out.println("Lock1");
            }
        }
    }
}

}

class MyLock {

public static final Object lock1 = new Object();
public static final Object lock2 = new Object();

}

second code, lock is not successful
public class Money {

public void say(){
    System.out.println("!");
}

}

public class Notebook {

public void say(){
    System.out.println("");
}

}

import java.util.concurrent.locks.Lock;

public class LockThread implements Runnable {

private boolean flag;
private Notebook hp;
private Money rmb;

public LockThread(Notebook hp, Money rmb, Boolean flag){
    this.hp = hp;
    this.flag = flag;
    this.rmb = rmb;
}

public void setFlag(boolean flag) {
    this.flag = flag;
}

public Notebook getHp() {
    return hp;
}

public void setHp(Notebook hp) {
    this.hp = hp;
}

public void setRmb(Money rmb) {
    this.rmb = rmb;
}

public Money getRmb() {
    return rmb;
}

public boolean isFlag() {
    return flag;
}

@Override
public void run() {
    if (flag){
        synchronized(hp){
            hp.say();
            synchronized (rmb){
                rmb.say();
            }
        }
    }else {
        synchronized (rmb){
            rmb.say();
            synchronized (hp){
                hp.say();
            }
        }
    }
}

}

public class Main {

public static void main(String[] args){
    Money rmb = new Money();
    Notebook hp = new Notebook();

    LockThread lock1 = new LockThread(hp, rmb, true);
    LockThread lock2 = new LockThread(hp, rmb, false);

    Thread thread1 = new Thread(lock1);
    Thread thread2 = new Thread(lock2);

    thread1.start();
    thread2.start();
}

}

Mar.06,2021

according to reason, your second code is likely to have a deadlock.
you don't have a deadlock. This may be because thread1 applied for rmb earlier than thread2 , because thread1.start () started

earlier than thread2.start () .
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b31031-2bd75.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b31031-2bd75.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?