I found the deadlock online
What is a deadlock:
A cross closed loop application occurred while applying for a lock. In this case, another thread has acquired lock B and must acquire lock A before releasing lock B. Therefore, A closed loop occurs and A deadlock cycle occurs.
Ok, if you can understand this sentence, there’s no need to read any further. You already know what a deadlock is
Four conditions for deadlock
1. Mutually exclusive conditions: Processes require exclusive control over allocated resources. That is, a certain resource is owned by only one process in a period of time. If another process requests the resource, the requesting process can only wait.
2. Request and hold conditions: the process has maintained at least one resource, but it makes a new request for the resource, which has been occupied by another process. At this time, the request process is blocked, but it does not release the obtained resource.
3. Inalienable conditions: The resources obtained by the process cannot be forcibly taken away by other processes before they are used up, that is, only the process that obtains the resources can release them (only proactively release them).
4. Circular wait condition: there is a circular wait chain of process resources, and the resources obtained by each process in the chain are simultaneously requested by the next process in the chain. That is, there is a set of processes in the waiting state {Pl, P2… , pn}, where the resource Pi is waiting for is occupied by P(I +1) (I =0, 1… , n-1), and the resource Pn waits for is occupied by P0
Well, if you still understand that sentence, then you really don’t need to read any more. You understand it very well. Combine these with some code examples below. You get the picture
But I didn’t get it, or I did at the time, and I forgot it after a while.
I understand deadlocks
Yeah, it’s like two guys pointing guns at each other, trying to get each other’s guns
Four conditions
Why is this my idea of deadlock? After all, to produce a deadlock, four conditions need to be met, so whether the deadlock I understand is met. So let’s do a one-to-one mapping. (The following four conditions are similar, but slightly different, as described in The Beauty of Java Programming.)
Tip: There are two different threads for men and women, and two threads for two resources
Four conditions for deadlocks in The Beauty of Java Programming
1. Mutually exclusive: A thread can use the obtained resource exclusively. That is, only one thread can use the resource at the same time
A handgun can only be held by one person, and someone else who wants to use it can only lose it or take it from the person who is using it. (Condition 1 is true)
2. Request and hold condition: a thread that has already owned at least one resource makes a request for a new resource, which has been occupied by another thread. Therefore, the current thread is blocked, but does not release the acquired resource
The man had already taken a gun, but he asked the woman to give it to him, and the gun was already taken by the woman. So he waits, and while he waits, he doesn’t give his gun to her. (Condition 2 is true)
3. Inalienable condition: Resources acquired by a thread cannot be preempted by other threads before it uses them up, and the thread can release the resources only after it uses them up.
Two men point guns at each other, and the other can take away his gun? The movie may have a leading role in the halo, but the two threads can not be the difference between big fish and small fish. (Condition 3 is true)
4. Loop wait condition: refers to the occurrence of deadlock, there must be a thread-resource ring chain, that is, the thread set {T0, T1,T2.. T0 in Tn} is waiting for T1, T1 is waiting for T2… Tn is waiting for the resources occupied by T0.
Obviously the man wanted the gun in the woman’s hand, and the woman wanted the gun in the man’s hand. (Condition 4 is true)
If you think that two people seem to think that condition 4 is a little confusing, then
(pretend minglou hand with a gun pointed at mingcheng.)
That, want to Ming tai hand gun, Ming tai want to Ming Lou hand gun, Ming Lou want to hand gun. You see, T1->T2->T3->T1 becomes a circular chain. It’s a ring to see where they stand.
Paper to learn zhongjue shallow, must know this to practice
In the code
The Beauty of Java Concurrent Programming
Private static Object gunA = new Object(); private static Object gunA = new Object(); private static Object gunB = new Object(); Public static void main(String[] agrs) {// Create a Thread. Thread threadMan = new Thread(new Runnable() {@override public void Run () {synchronized (gunA) {system.out.println (thread.currentthread () + "man got gunA "); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } system.out.println (thread.currentThread () + "man wants gun B"); Synchronized (gunB) {system.out.println (thread.currentThread () + "synchronized (gunB) "); }}}}); Thread threadWoMan = new Thread(new Runnable() { @Override public void run() { synchronized (gunB) { System.out.println(thread.currentThread () + "woman got gun B"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } system.out.println (thread.currentThread () + "woman wants gun A"); Synchronized (gunA) {system.out.println (thread.currentThread () + "synchronized "); }}}}); Threadman.start (); threadWoMan.start(); }}Copy the code
Running result:
Thread[thread-1,5,main] The man got the gun A Thread[thread-1,5,main] The woman got the gun B Thread[thread-1,5,main] The woman wanted the gun A Thread[thread-0,5,main] Man wants gun BCopy the code
A man and a woman point a gun at each other, and each tries to grab the other’s gun, but there’s no word. This is a deadlock.
Code analysis:
The code first creates two guns, that is, two resources, and creates two threads (thread-0 is male, thread-1 is female). After the man gets a gun, the thread sleeps for 1s so that the woman can get the gun as well. In this case, the man acquires gunA and the woman acquires gunB (thread 0 acquires the lock on resource gunA and thread 1 acquires the lock on resource gunB). After the man gets gun A, the view gets gun B, which is already occupied by the woman, and the woman gets gun A from the view. Obviously not. So I’m stuck waiting. This creates a deadlock.
How do I break a deadlock
To perfect the scene above, two people find a door to a bug-free world, but the door is closed and two guns are placed in the door. Only when you get both guns can you enter the door (after you enter, the two guns return to the original place). According to the plot of the TV series, is this what will happen? One person after a gun, complementary compromise.
So how can we avoid this deadlock and let both people lead to the buggy world? In fact, one person can go in first with two guns, and the other person can get them again.
Therefore, to break a deadlock, only one of the four conditions can be broken, and the only conditions that can be broken are condition 2, “ask and hold,” and condition 4, “loop wait.”
The reason for the deadlock is very similar to the door-to-door scenario, and has a lot to do with the order in which resources are requested. If two people go in order to get guns in the door. Deadlock is avoided. How does the code represent that? Modify the thread WoMan
Thread threadWoMan = new Thread(new Runnable() { @Override public void run() { synchronized (gunA) { System.out.println(thread.currentThread () + "woman got A"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } system.out.println (thread.currentThread () + "woman wants gun B"); Synchronized (gunB) {system.out.println (thread.currentThread () + "synchronized "); }}}});Copy the code
The results
Thread/Thread - 0, 5, the main man got the gun A Thread/Thread - 0, 5, the main man want gun Thread B/Thread - 0, 5, the main man B got A gun Thread/Thread - 1, 5, the main women got robbed A Thread/Thread - 1, 5, the main women want gun Thread B/Thread - 1, 5, the main women got the gun BCopy the code
Code analysis:
Both men and women go for gun A. If the man gets gun A first, the woman will be blocked and will not get gun B. After the man gets gun A, he will get gun B, and he will also get it smoothly, entering A bug-free world. At this point the two guns are back where they were (the locks on the two gun resources are released). Woman can go get gun A and get gun B.
Here are some small points:
- 1. After the male and female threads start. You don’t have to start to get resource gun A first
- 2. If the male thread acquires gun A, the female thread will not acquire gun B, because the female thread needs to acquire gun A first, and the male thread acquires gun A, the female thread will be blocked
- 3. Male thread does not release gun A when it acquires gun B. The misunderstanding here is that a thread can only acquire one resource at a time, i.e. a thread can only hold one lock at a time. The answer is: No. Because locks are for “objects”, and the locks and objects are one-to-one, threads can hold multiple locks at the same time.