Home >Java >javaTutorial >The concept and solution of deadlock in Java

The concept and solution of deadlock in Java

零下一度
零下一度Original
2017-07-20 13:41:312872browse

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:

3. A simple code about deadlock:
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:

4. Supplement:
I found some information about deadlock , I feel that the issue here is quite complicated. At this stage only these simple principles are understood. We can also roughly understand that four certain conditions must be met for deadlock to occur.
1) Mutual exclusion condition:
refers to the exclusive use of the allocated resources by a process, that is, a certain resource is only occupied by one process within a period of time. If there are other processes requesting resources at this time, the requester can only wait until the process occupying the resources is released.
2) Request and retention conditions:
means that the process has maintained at least one resource, but has made a new resource request, and the resource has been occupied by other processes. At this time, the requesting process is blocked. , but still hold on to other resources that they have obtained.
3) Non-deprivation condition:
refers to the resource that the process has obtained. It cannot be deprived before it is used up, and can only be released by itself when it is used up.
4) Loop waiting condition:
means that when a deadlock occurs, there must be a process-a circular chain of resources, that is, the process set {P0, P1, P2,... , P0 in Pn} is waiting for a resource occupied by P1; P1 is waiting for a resource occupied by P2,..., Pn is waiting for a resource occupied by P0.
Attachment:
1) Calling the sleep() method will not allow the thread to release the synchronization lock it holds; and during this period it will not Prevent other threads from running
2) Call the wait() method, and the currently running thread will enter the waiting state, waiting for other threads to call the notify() or notifyAll() method of this object again to wake it up. Or when the specified waiting time is reached, the thread wakes up automatically.
If a thread owns a synchronization lock on one or more objects, then after calling wait(), the thread will release all synchronization resources it holds, not limited to the one that has been called wait() ) method object.
3) In actual development, deadlocks are generally hidden deeply and are not easy to be discovered. Once a deadlock occurs, it will inevitably lead to program paralysis. Therefore it must be avoided.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn