Deadlock:
Concept:
The so-called deadlock: refers to two or more than two Process in the process of execution, because of the contention for the resources of a mutual waiting phenomenon, if there is no external force, they will not be able to proceed. At this point the system is in a deadlock state or the system generates a deadlock, and these processes, which are always waiting on each other, are called deadlock processes. Because resource consumption is mutually exclusive, when a process requests resources, so that the process without external assistance, can never allocate the necessary resources and can not continue to run, which creates a special phenomenon: deadlock.
Conditions for deadlocks to occur:
1. Mutex condition: A resource can only be used by one thread at a time
2. Non-preemption conditions (non-deprivation): The current process locks the resources, before the end, cannot forcibly deprive
3. Possession and application conditions (request and hold conditions): a process has a certain amount of resources, but also want to apply for other resources, but not to their own resources to give up
4. Cycle conditions: A relationship between a number of processes forming a tail-to-toe cyclic waiting resource
When does it usually appear? Nested synchronization in synchronization
Create a deadlock:
Class Test2 implements runnable{private Boolean flag; Test2 (Boolean flag) {This.flag = flag; } @Override public void Run () {//TODO auto-generated method stub if (flag) {sy Nchronized (Mylock.locka) {System.out.println (Thread.CurrentThread (). GetName () + "if Locka"); Synchronized (mylock.lockb) {System.out.println (Thread.CurrentThread (). GE Tname () + "if lockb"); }}} else {synchronized (mylock.lockb) {System.out . println (Thread.CurrentThread (). GetName () + "Else lockb"); Synchronized (Mylock.locka) {System.out.println (Thread.CurrentThread (). GetName () + "Else Locka "); }}}}}class mylock{static Object Locka = new Object (); Static Object lockb = new Object ();} public class Dielocktest{public static void Main (string[] args) {Thread T1 = new Thread (new Test2 (true)); Thread t2 = new Thread (new Test2 (false)); T1.start (); T2.start (); }}
The program that verifies the synchronization function before the lock is this also becomes a deadlock:
Class Test1 implements runnable{private static int num = 500; Object obj = new Object (); Boolean flag = true; public void Run () {if (flag) {while (true) {synchronized (obj)/ /Lock is obj {show ();//Lock Is this}}} else { while (true) {show (); }}} public synchronized void Show ()//Lock is this {synchronized (obj)//Lock is obj { if (num >= 0) {try {thread.sleep (20); } catch (Exception e) {//Todo:handle Exception Syst Em.out.println (E.tostring ()); } System.out.println (Thread.CurrentThread (). GetName () + ">>--code--" +num--); }}}}public Class dielock{publicstatic void Main (string[] args) {Test1 t = new Test1 (); Thread A = new thread (t); Thread B = new Thread (t); A.start (); try {thread.sleep (20); } catch (Exception e) {//Todo:handle Exception} T.flag = false; B.start (); }}
Prevention of deadlocks:
The prevention of deadlock is a strategy to ensure that the system does not enter the deadlock state.
Know the conditions of the deadlock, to avoid the deadlock will start from breaking the conditions.
Click to open link
Java------multi-threaded---deadlock