In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "java concurrent programming deadlock definition and deadlock avoidance case analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "java concurrent programming deadlock definition and deadlock avoidance case analysis" can help you solve the problem.
Scene simulation analysis scenario 1: narrow paths meet
At 10:30 on Sunday morning, you are driving on the highway, which is a narrow road that can only hold one car. At this time, there was another car coming head on, and you were all in the middle of it, and no one wanted to go back, so they didn't give way. Caught up in endless waiting.
Scene 2: cold War
You quarreled with her, ignored each other, and even cooked at dinnertime. You are frying Sautéed Shredded Pork in Sweet Bean Sauce, and she is making roasted crucian carp with scallion. In the middle of stir-frying, you find that she has taken all the spring onions, so you wait silently for her to cook the dishes before you get them. Do not realize that she is also waiting for you to stir-fry and then take soy sauce.
Scene 3: philosopher Dinner
You sit at a round table with four good friends, and you only do two things: eat, or think. When you eat, you stop thinking. When you think, stop eating. There is a bowl of Lanzhou fried rice in front of everyone. And everyone has a chopstick on the left and right. You must get two chopsticks before you can start eating. Put down the chopsticks after eating so that others can use them. After eating for a while, everyone picked up the chopsticks on their left-hand side, so that everyone had only one chopstick and waited for others to put down the chopsticks for themselves. Unfortunately, no one can eat, so no one will put down their chopsticks. (the famous philosopher dining problem)
Scenario 4: competing for resources
You have two threads An and B, each running in a locked state. A holds part of the resources and waits for the resources in thread B to complete its work, while thread B is also waiting for resources in A to finish its work. Since they are all locked in, they must finish their work before the resources they hold can be released. So the thread waits endlessly, resulting in a deadlock.
What's the deadlock?
The above four scenarios are all problems that programmers will encounter in their work or life. Life is like a process, time is our main thread, and everything we do is a child thread. Concurrency problems arise when multiple things conflict. The above scenarios all point to the same type of concurrency problem: deadlocks.
When there are more than two computing units, both sides are waiting for the other to stop running in order to obtain system resources, but no one exits in advance, it is called deadlock.
The four conditions that lead to deadlocks are as follows:
1. Mutually exclusive condition: a resource can only be used by one process at a time.
2. request and retention conditions: when a process is blocked by a request for resources, it holds on to the resources it has acquired.
3. No deprivation condition: the resources that have been acquired by the process cannot be forcibly deprived until they have been used up.
4. Loop waiting condition: a relationship in which multiple processes cycle waiting for resources is formed.
Concurrency brings stress, and some people or programs will collapse because they can't bear the pressure. Emotional collapse is no different from program collapse. Of course, whether it is to be a person or write a program, the right thing to do in the face of problems is to adopt strategies to release the deadlock.
How to avoid deadlocks?
Once a deadlock occurs, it cannot be released. All we can do is try to avoid deadlocks. To avoid deadlocks, you only need to break one of the four conditions that give rise to deadlocks.
Option 1: destroy the conditions of non-deprivation
You remember what the book says: take a step back. But you also know that fairness is better than tolerance. When it is time for the weekly race, you roll down the window and shout to the brother opposite: let's compete in a weekly race and let someone else pass who loses. So you open the buckle and begin to answer the question. Half an hour later, you AC all the questions with your excellent code level. The opposite driver said to you, "if your skills are not as good as others, you are willing to be outdone." So he went back and gave up half of his way. In the end, you all got through smoothly.
Destroy the condition of non-deprivation: let the driver on the other side give up his existing resources.
Option 2: destroy the request and hold conditions
When you are cooking, you find that there are no spring onions, so you think for the sake of others and think that she may also lack the materials she uses. Although she is still having a cold war with you, you persuade yourself that a man should not be angry with a girl, so you take the initiative to give her all the materials you use. She was deeply moved by the feeling that you put yourself in her shoes and that you were back together. Then she fried Sautéed Shredded Pork in Sweet Bean Sauce and roasted crucian carp with green onions for both of you.
Destroy the request and retention conditions: when the materials you need are lacking, take the initiative to give up your own resources to prevent them from waiting for each other.
Plan 3: destroy the cycle waiting condition
You and your friends decide to number the chopsticks: 1: 5. It is stipulated that when holding chopsticks, everyone must first get the smaller one of the chopsticks on both sides, and then get the big one. If you don't get the small one, you can't get the big one first. When you start eating, because the number 5 cannot be taken by one person alone. Because the number of the other chopstick next to him must be less than 5, there will be no more infinite waiting for everyone with one.
Break the loop wait condition: because the chopsticks specify the number and acquisition rules, each lock state is executed sequentially, so the loop wait condition is eliminated.
Plan 4: destroy the mutually exclusive condition
Before you run two threads, you make a copy of the resources in threads An and B in advance so that they don't have to wait for each other's resources, so both threads run smoothly.
Break the mutex condition: since a copy is made each time it is used, a resource can be used by multiple processes.
This is the end of the introduction to "deadlock definition and deadlock avoidance case Analysis in java concurrent programming". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.