What is a deadlock
Let’s first look at such a life example: there is a bridge on a river, with a narrow bridge deck that can only accommodate one car and cannot allow two cars to go in parallel. If two cars A and B drive onto the bridge from both ends of the bridge, then for Car A, it walks across a section of the road on the left side of the bridge deck (that is, it occupies part of the resources of the bridge). If you want to cross the bridge, you have to wait for Car B to give up the bridge deck on the right. Car A cannot move forward at this time; for Car B, it walks across a section of the road on the right side of the bridge deck (that is, it occupies part of the resources of the bridge). If you want to cross the bridge, you have to wait for Car A to give up the bridge deck on the left, and Car B cannot move forward at this time. The cars on both sides did not reverse, which resulted in waiting for each other to give up the bridge deck, but no one gave way and would wait endlessly. This phenomenon is deadlock. If the car is compared to a process and the bridge deck is used as a resource, then the above problem is described as: Process A possesses resource R1, waiting for resource Rr occupied by process B; Process B possesses resource Rr, waiting for resource R1 occupied by process A. Moreover, resources R1 and Rr only allow one process to occupy, that is, two processes are not allowed to occupy at the same time. As a result, neither process can continue to execute. If no other measures are taken, this cycle waiting situation will continue indefinitely, and a process deadlock will occur.
In computer systems, software and hardware resources may be deadlocked. For example: There is only one CD-ROM driver and one printer in the system. One process owns the CD-ROM driver and applies for a printer; the other process owns the printer and applies for a CD-ROM. As a result, both processes are blocked and can never be disconnected on their own.
The so-called deadlock refers to a situation where multiple processes cycle through the resources they occupy and remain in a stalemate indefinitely. Obviously, if there is no external force, then all processes involved in deadlock will always be blocked. From the above example, we can see that the fundamental reason for deadlocks in computer systems is limited resources and improper operation. That is: one reason is that the system provides too few resources and is far from meeting the resource requirements of concurrent processes. This deadlock caused by competing resources is the core of our discussion. For example: Message is a temporary resource. At some point, process A is waiting for a message sent by process B, process B is waiting for a message sent by process C, and process C is waiting for a message sent by process A. If the message has not arrived, none of the three processes A, B, and C cannot move forward, and deadlocks will also occur in process communication. Another reason is deadlocks caused by inappropriate process advancement order. Small resources may not necessarily lead to deadlocks. Just like two people crossing a single-plank bridge, if both of them have to pass first and are stalemate on the single-plank bridge, they will inevitably lead to a deadlock for competition resources; however, if two people first check whether there are other people on the bridge before going to the bridge, and only go up the bridge by themselves when there are no other people on the bridge, then the problem will be solved. Therefore, if the program is designed unreasonably and the process is promoted inappropriately, it will also cause deadlocks.
Deadlock
Only when t1 thread occupies o1 and also requires o2, and t2 takes o2 at this time and also requires o1 will there be a deadlock. (Similar to two people eating with two chopsticks, they both need one chopstick from the other party to eat)
The following code: The t1 thread occupies o1 and only releases o1 after obtaining the o2 object. The t2 thread occupies o2 first and then acquires o1. At this time, o1 is occupied by the t1 thread, o2 is occupied by the t2 thread, t1 and t2 are waiting infinitely, and a deadlock will occur.
package javasimple;/** * Deadlock demo * @author haokui * */public class DieSynchronized { public static void main(String[] args) { /** * Create and start two threads t1 and t2. Both threads must share two objects o1 and o2*/ Object o1 = new Object(); Object o2 = new Object(); Thread t1 = new Thread(new T1(o1,o2)); Thread t2 = new Thread(new T2(o1,o2)); t1.start(); t2.start(); }}//Create two thread classes T1 implements Runnable { Object o1; Object o2; public T1(Object o1, Object o2){ this.o1 = o1; this.o2 = o2; } public void run() { //Locks o1 and o2 synchronized (o1) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (o2) { System.out.println("o2"); } } } } } } } class T2 implements Runnable { Object o1; Object o2; public T2(Object o1, Object o2) { this.o1 = o1; this.o2 = o2; } public void run() { synchronized (o2) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (o1) { System.out.println("o1"); } } }} Note: Concurrency occurs only when o1 and o2 are shared. Two objects can be shared through constructors.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.