JAVA multi-threaded deadlock

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42947972/article/details/102776765

example:

class Test implements Runnable{
    private boolean flag;
    Test(boolean flag) {
        this.flag = flag;
    }

    public void run(){
        if (flag){
            synchronized (MyLock.locka){
                System.out.println("if.....locka....");
                synchronized(MyLock.lockb){
                    System.out.println("if.....lockb....");
                }
            }
        }
        else{
            synchronized (MyLock.lockb){
                System.out.println("else.....lockb....");
                synchronized(MyLock.locka){
                    System.out.println("else.....locka....");
                }
            }
        }
    }
}
class MyLock {
    public static final Object locka = new Object();
    public static final Object lockb = new Object();
}
public class Demo{
    public static void main(String[] args){
        Test a = new Test(true);
        Test b = new Test(false);
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);
        t1.start();
        t2.start();
    }
}

Started by the main thread thread

 Test a = new Test(true);
 Test b = new Test(false);

Then put their addresses are given to the t1 t2Thread object created

 		Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);

If you do not give, then

	 t1.start();
	 t2.start();

Is the default run executed, meaningless ...
.
.
.
.
.
The main thread main execution

 Test a = new Test(true);
 Test b = new Test(false);

Two of them so that flagthese tags are not the same, a is a true, b isfalse

Then main main thread continue to go down until the two threads open

		t1.start();
        t2.start();

At this time, t1 is of a true
cpu also just execute it, so he entered into

if (flag){
            synchronized (MyLock.locka){
                System.out.println("if.....locka....");
                synchronized(MyLock.lockb){
                    System.out.println("if.....lockb....");
                }
            }
        }

Get the MyLock.lockalock
that is public static final Object locka = new Object();
outputif.....locka....

This time cpu suddenly not perform a thread t1, t2 threads to execute a
(because the cpu is performed random !!!)
because the second is the implementation of the so fals

else{
            synchronized (MyLock.lockb){
                System.out.println("else.....lockb....");
                synchronized(MyLock.locka){
                    System.out.println("else.....locka....");
                }
            }
        }

This time got the MyLock.lockblock
is public static final Object lockb = new Object();
then perform output: else.....lockb....
when you want to enter the synchronized(MyLock.locka)statement, took t1 lock has been found, and this has been repeatedly get in ...

Until cpu re-randomized to t1
threads to execute synchronized(MyLock.lockb)the statement, but the synchronized(MyLock.lockb)statement has been t2 took

this time t1 repeatedly get in ...
.
.

Until cpu re-randomized to t2, so who can not get back and forth, leading to the entrance ... multithreading deadlock

T2 is sometimes grab the first execution of the CPU ... but the results are the same can not get out, could not come ...
is the output just not the same ...

Some people also may be able to execute successfully, but only in the ideal state ... may try to add a cycle where a deadlock occurs sooner or later ...

FIG running effect (1):

T1 This is the first implementation of the right to grab the cpu
Here Insert Picture Description
This will not go down ...
.
.
.
.
.

FIG running effect (2):

T2 This is the first implementation of the right to grab the cpu
Here Insert Picture Description
so it will not go down ...

Guess you like

Origin blog.csdn.net/weixin_42947972/article/details/102776765