1. Process deadlock and solutions
1. Key points
(1) Master the concept of deadlock and the occurrence of deadlock The root cause of the lock.
(2) Understand the necessary conditions for deadlock - the following four conditions must be met at the same time: mutual exclusion condition, non-preemption condition, possession and application condition, and loop waiting condition.
(3) Remember the general methods of solving deadlocks, and master the basic ideas of deadlock prevention and deadlock avoidance.
(4) Master the orderly allocation strategy of resources in the deadlock prevention strategy.
(5) Understand the concept of process safety sequence and the relationship between deadlock and safety sequence.
(6) Understand the banker’s algorithm.
(7) Understand the resource allocation diagram.
(8) Understand the ideas of deadlock detection and recovery.
2. Regarding deadlock, my personal understanding:
After a period of study, the concept of deadlock that I understand is that, for example, there are two processes, each A process is locked by two locks (Lock 1, Lock 2). The difference is that the lock application locations are different in the two processes. As a result, the complete startup of a process requires an internal lock, and this lock happens to be in another process. The lock that another process needs to completely start (or execute) the internal process is in another program. In this way, they are tied to each other, and they are all in a state of preparation but unable to execute. Caused a deadlock state. I drew a schematic diagram based on my understanding:
public class Test15 {public static void main(String[] args) throws InterruptedException {new Thread(new DeadLockThread(true)).start();//Thread.sleep(10);在中间用上它可以使结果交替出现,我是为了看死锁的效果,用它对比一下。new Thread(new DeadLockThread(false)).start(); } }class DeadLockThread implements Runnable {static Object o1 = new Object();static Object o2 = new Object();private boolean flag; DeadLockThread(boolean flag) {this.flag = flag; }public void run() {if (flag == true) {while (true) {synchronized (o1) { System.out.println("这是锁o1");synchronized (o2) { System.out.println("这是锁o2"); }
Print result:
The above is the detailed content of The concept and solution of deadlock in Java. For more information, please follow other related articles on the PHP Chinese website!