If you encounter the concept of Java thread deadlock

Multi-threading and multi-process improve the utilization of system resources and the processing capacity of the system. However, concurrent execution also brings a new problem-deadlock (DeadLock).

The so-called deadlock refers to a deadlock caused by multiple threads competing for resources (to eat with each other, oh don't wait for each other ). If there is no external force, these processes will not be able to move forward.

You can also refer to this excellent article: You have wine in your dimples-thread deadlock


Common deadlock solutions:

    (1) Destruction request and retention conditions: apply for all resources at once, and no longer apply for resources afterwards. If the resource conditions are not met, no resource allocation will be obtained. Only get the initial resources to run, and then release the finished resources and request new resources.

    (2) Destruction of the non-preemptive condition: When a process wants to obtain a certain non-preemptable resource, it will apply for a new resource. If it cannot be satisfied, all resources will be released, and if needed later, apply again.

    (3) Disrupt the loop waiting condition: sort the resources, and request the resources in the order of increasing sequence number. If a process obtains a resource with a high sequence number and wants to obtain a resource with a low sequence number, it needs to release the resource with a high sequence number first.


Tips: The IDEA tool can use jps -l to display the running virtual machine process, or use jstack + thread ID to track and troubleshoot problems.


Here are a few commonly used methods to prevent threads from serving each other without waiting for each other:

Avoid thread deadlock-break the deadlock of holding separately and waiting for each other

Avoid thread deadlock-allow threads to seize resources

Avoid thread deadlock-terminate thread loop waiting

Guess you like

Origin blog.csdn.net/qq_44965393/article/details/112728440