On the deadlock problem of Java multithreading

On the deadlock problem of Java multithreading

Deadlock is an important part of java multithreading, and the understanding of deadlock is very important.

What is a deadlock:
The official definition of Banzheng is:
when multiple threads occupy the synchronization resources needed by other threads, and are waiting for other threads to release the synchronization resources they need, a deadlock is formed.
This example is not easy to understand, please see the following example:
Insert picture description here

As shown in the above picture, there are two little people a and b. There is a table and two bowls of rice on the table in front of them, but there is only one pair of chopsticks. Then the problem is that everyone can only eat with one pair of chopsticks, which is a First take the chopsticks to eat, and then give the chopsticks to b, so that both of them can finish the meal smoothly.
So what is the deadlock? That is, if a is holding a chopstick and b is holding a chopstick, and neither of them will give it to anyone, then there will be a stalemate and no one can eat.

Phenomenon
after deadlock : After deadlock: no exceptions or prompts will appear, but all threads will be blocked and cannot continue.

Below we use java code to demonstrate the appearance of deadlock:

public class deadLock {
    
    
	public static void main(String[] args) {
    
    
		StringBuffer s1=new StringBuffer();
		StringBuffer s2=new StringBuffer();
		 new Thread() {
    
    
			 @Override
			public void run() {
    
    
				// TODO Auto-generated method stub
				synchronized(s1) {
    
    
					s1.append("a");
					s2.append("1");
					
					try {
    
    
						sleep(1000);
					} catch (InterruptedException e) {
    
    
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					synchronized(s2) {
    
    
						s1.append("b");
						s2.append("2");
						System.out.println(s1);
						System.out.println(s2);
					}
				}
			}
		 }.start();
		 
		 new Thread(new Runnable() {
    
    
			 @Override
			public void run() {
    
    
				// TODO Auto-generated method stub
				synchronized(s2) {
    
    
					s1.append("c");
					s2.append("3");
					
					try {
    
    
						Thread.sleep(1000);
					} catch (InterruptedException e) {
    
    
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					synchronized(s1) {
    
    
						s1.append("d");
						s2.append("4");
						System.out.println(s1);
						System.out.println(s2);
					}
				}
			}
		 }).start();
	}
}

After running, you will find that there will be no output.
This is because we use the synchronized synchronization code block.
The StringBuffer defined first is thread-safe. s1 and s2 are critical resources.
Thread 1 holds the lock of s1 and asks for s2,
while thread 2 holds the lock of s2 and asks for s1,
thus forming a deadlock.

Guess you like

Origin blog.csdn.net/qq_45273552/article/details/109085821