Examples and solutions of java thread deadlock
This article mainly introduces java thread deadlock examples and solutions. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
1. Definition of deadlock
Deadlock refers to a deadlock (waiting for each other) caused by multiple threads competing for resources. Without external force, these processes will not be able to move forward
2. Necessary conditions for deadlock generation
Mutually exclusive condition: the thread requires exclusive control over the allocated resources (such as printers), that is, a resource is occupied by only one thread for a period of time. At this time, if a thread requests the resource, the requesting thread can only wait.
No deprivation condition: the resource obtained by a thread cannot be taken away by other threads before it is used up, that is, it can only be released by the thread that obtains the resource itself (it can only be released actively).
Request and hold condition: the thread has held at least one resource, but has made a new resource request, and the thread has been occupied by other threads. At this time, the requesting process is blocked, but it does not let go of the resources it has obtained.
Circular waiting condition: there is a circular waiting chain of thread resources. The resources obtained by each thread in the chain are requested by the next thread in the chain at the same time. That is, there is a thread set {P1, P2,..., PN} in the waiting state, in which the resources waiting for PI are occupied by P (I + 1) (I = 0,1,.., n-1), and the resources waiting for PN are occupied by P0, as shown in the following figure.
3. An example of deadlock generation
/** * 一个简单的死锁类 * 当DeadLock类的对象flag==1时(td1),先锁定o1,睡眠500ms * 而td1在睡眠的时候另一个flag==0的对象(td2)线程启动,先锁定o2,睡眠500ms * td1 睡眠结束后需要锁定 o2 才能继续执行,而此时 o2 已被 td2 锁定; * td2 睡眠结束后需要锁定 o1 才能继续执行,而此时 o1 已被 td1 锁定; * td1、td2 相互等待,都需要得到对方锁定的资源才能继续执行,从而死锁。 */ public class DeadLock implements Runnable { public int flag = 1; //静态对象是类的所有对象共享的 private static Object o1 = new Object(),o2 = new Object(); @Override public void run() { System.out.println("flag="+flag); if(flag==1){ synchronized (o1){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2){ System.out.println("1"); } } } if(flag==0){ synchronized (o2){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1){ System.out.println("0"); } } } } public static void main(String[] args) { DeadLock td1 = new DeadLock(); DeadLock td2 = new DeadLock(); td1.flag=1; td2.flag=0; //td1,td2都处于可执行状态,但JVM线程调度先执行哪个线程是不确定的。 //td2的run()可能在td1的run()之前运行 new Thread(td1).start(); new Thread(td2).start(); } }
4. How to avoid deadlock
In some cases, deadlocks can be avoided. Two techniques for deadlock avoidance
1) Lock sequence (threads lock in a certain sequence)
2) Lock time limit (a certain time limit is added when a thread obtains a lock. If the time limit is exceeded, it will give up the request for the lock and release the lock it owns)
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.