Java implementation deadlock example code
What is deadlock
Let's take a look at an example in life: there is a bridge on a river with a narrow bridge deck, which can only accommodate one car, and can't let two cars go parallel. If two cars a and B drive onto the bridge from both ends of the bridge, for car a, It passes a section of Road on the left of the bridge deck (that is, it occupies part of the resources of the bridge). If you want to cross the bridge, you must wait for vehicle B to give up the bridge deck on the right. At this time, vehicle a cannot move forward; for vehicle B, it passes a section of Road on the right of the bridge deck (that is, it occupies part of the resources of the bridge). If you want to cross the bridge, you must wait for car a to give up the bridge deck on the left. At this time, car B can't move forward. Cars on both sides don't reverse, resulting in waiting for each other to give up the bridge deck, but if no one gives up the road, you will wait endlessly. This phenomenon is deadlock. If you compare the car to a process and the bridge deck as a resource, then the above problems will be solved Description: process a occupies resource R1 and waits for resource RR occupied by process B; Process B occupies resource RR and waits for resource R1 occupied by process a. Moreover, resources R1 and RR are only allowed to be occupied by one process, that is, two processes are not allowed to be occupied at the same time. As a result, both processes cannot continue to execute. If no other measures are taken, this circular waiting condition will continue indefinitely, and a process deadlock occurs.
In a computer system, both software and hardware resources may deadlock. For example, there is only one CD-ROM drive and one printer in the system. A process occupies the CD-ROM drive and applies for a printer; Another process took possession of the printer and applied for a CD-ROM. As a result, both processes are blocked and can never be released by themselves.
Deadlock refers to the situation in which multiple processes are deadlocked indefinitely while waiting for the resources occupied by others. Obviously, if there is no external force, the processes involved in the deadlock will always be blocked. As can be seen from the above example, the root cause of deadlock in computer system is limited resources and improper operation. That is, one reason is that the resources provided by the system are too few to meet the resource requirements of concurrent processes. This deadlock caused by competing resources is the core of our discussion. For example, a message is a temporary resource. At some point, process a waits for a message from process B, process B waits for a message from process C, and process C waits for a message from process a. If the message does not arrive, processes a, B and C cannot move forward, and deadlock in process communication will also occur. Another reason is the deadlock caused by improper process advance sequence. Fewer resources may not necessarily lead to deadlock. Just like two people crossing a single wooden bridge, if two people have to cross first and refuse to retreat on the single wooden bridge, there will inevitably be a deadlock in competing resources; However, if two people look at each other's people on the bridge before they get on the bridge, and they don't get on the bridge until they have no other people on the bridge, the problem will be solved. Therefore, if the program design is unreasonable, resulting in the improper sequence of process advancement, deadlock will also occur.
deadlock
Deadlock occurs only when T1 thread occupies O1 and needs O2, and T2 occupies O2 and needs O1. (similar to two people eating with two chopsticks, they both need one of the other's chopsticks.)
In the following code, thread T1 occupies O1 and releases O1 only after obtaining the O2 object, while thread T2 first occupies O2 and then obtains O1. At this time, O1 is occupied by thread T1 and O2 is occupied by thread T2. Both T1 and T2 are waiting indefinitely, and a deadlock will occur.
Note: concurrency occurs only when O1 and O2 are shared. Two objects can be shared by constructor.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.