There is usually some sort of balance between security and activity. We use the lock mechanism to ensure security, but unreasonable use of locks can lead to lock-order deadlocks, and we use thread pools and semaphores to limit the use of resources, but these restrictions can lead to resource deadlocks.
Java applications cannot recover from deadlocks (which can cause performance degradation or even no response, unlike a database that has a solution for deadlocks, which can only restart services), so design must exclude those conditions that could lead to deadlocks;
This article describes some of the causes of active failure and how to avoid 1: Deadlocks
The so-called deadlock refers to a number of processes due to competitive resources caused by a deadlock (mutual waiting), without external forces, these processes will not be able to move forward;
You can look here, there are deadlocks related to the introduction: http://see.xidian.edu.cn/cpp/html/2604.html
1.1 Lock Order deadlock Two threads try to get the same lock in a different order, for example, locks L and M, thread a request a lock in the order of l/m, thread B requests a lock in the order of M/L, a lock order deadlock may occur, and if a lock is requested in the same order, the lock dependency is not present , it will not produce lock sequence deadlock;
public class leftrightlock{
private Final Object Left=new object ();
Private Final Object Right=new object ();
public void LeftRight () {
synchronized (left) {
synchronized (right) {
<span style= "White-space:pre" > </span> dosomething ();
}} public void Rightleft () {
synchronized (right) {
synchronized (left) {
dosomething ();
}
}}
}
1.2 Dynamic Lock order deadlock
public void TransferMoney (account Fromaccount,account toaccount,int) {
synchronized (fromaccount) {
Synchronized (toaccount) {
dosomething ();}}}
Deadlocks may occur and appear to be locked in a fixed order, but the actual lock order is related to the parameters passed in. If two threads are executing at the same time: TransferMoney (fromaccount,toaccount,10)
TransferMoney (toaccount,fromaccount,20) is possible to resolve deadlocks. To lock in a fixed order throughout the program
The public static final object Tielock=new object () is public
void TransferMoney, account Fromaccount,account Toaccount,int Money) {
int fromhash=fromaccount.identityhashcode (fromaccount)
int Tohash=toaccount.identityhashcode ( Toaccount)
if (fromaccount<toaccount) {
synchronized (fromaccount) {
synchronized (toaccount) {
DoSomething ();}}
else if (fromaccount>toaccount) {
synchronized (toaccount) {
synchronized (fromaccount) {
DoSomething ();}}
else{
synchronized (tielock) {
synchronized (toaccount) {
synchronized (Fromaccount)
{ DoSomething ();}}}
1.3 Deadlock occurs between collaboration objectsIf you call an external method while holding a lock, you may have an active problem. Other locks may be acquired in this external call (which may result in deadlocks), or the blocking time is too long, causing the other threads to fail to obtain the currently held lock;
Class a{
private b b;
Public synchronized void F () {
dosomethinga ();
DOSOMETHINGB ();
B.method ();
}
1.4 Open CallsIf you do not need to acquire a lock when you invoke a method, this call is called an open call, and the open call resolves the above phenomenon of deadlock between the collaborating objects; shrinking the lock code block, which can lose the atomic nature of the operation.
Class a{
private b b;
public void F () {
synchronized{
dosomethinga ();
DOSOMETHINGB ();
}
B.method ();
}
2: Avoidance of deadlocks with diagnostics 2.1 Support for timed locks 2.2 Analysis of deadlock through thread dump information 3: Other active danger 3.1 HungerHunger occurs when a thread is unable to access the resources it needs to continue execution, and the most common source of hunger is the CPU. If the Java program does not use the priority of the thread or holds the lock, perform some structures that cannot be terminated (for example, infinite loops, unlimited waiting for a resource) can also cause thread starvation; you should avoid using thread priorities, which increases platform dependencies and leads to active problems, preferably using the default thread priority
3.2 Poor responsiveness 3.3 Live Lock