Deadlock:
In the case of multi-threaded contention using shared resources. There is a possibility of a deadlock. For example, when a thread waits for a lock that is held by one of the threads. The thread may be waiting for the lock held by the first thread. At this time These two threads are stuck in endless mutual wait states. Such a situation is called a deadlock.
The four necessary conditions for creating a deadlock:
1, mutually exclusion conditions.
The process has exclusive control over the allocated resources, that is, for a period of time, a resource is occupied only by a process.
2, request and maintain conditions. When a process is blocked by requesting a resource, it remains in place for the resources that have been obtained.
3. No deprivation of conditions.
The resource that the process has obtained before it is exhausted. cannot be deprived. Can only be released by yourself when you are finished using it.
4, loop waiting conditions.
In the event of a deadlock, there must be a process-a circular chain of resources. That is, the process collection {P1,P2, ...
, P1 in Pn} waits for a resource that P2 occupies, P2 is waiting for a resource to be occupied by a P3.
。
。 , PN is waiting for resources that have been consumed by P1.
A simpler way to resolve deadlocks is to destroy one or more of the four necessary conditions that create deadlocks by setting certain constraints. To prevent deadlocks from occurring. The prevention of deadlocks is a more easily implemented method and has been widely used. But because the restrictions imposed are often too restrictive. May cause system resource utilization and system throughput to be reduced.
The following is a demo sample that produces a deadlock:
Package Com.hh.deadlock;public class Deadlockdemo {public static void main (string[] args) {final Object Resource1 = "Resource 1"; Final Object Resource2 = "Resource 2"; thread T1 = new Thread () {public void run () {synchronized (Resource1) {System.out.println ("Thread 1: Get resource 1 use right"); try { Thread.Sleep (500);} catch (Exception e) {}synchronized (Resource2) {System.out.println ("Thread 1: Waiting for resource 2");}}}; Thread t2 = new Thread () {public void run () {synchronized (RESOURCE2) {System.out.println ("Thread 2: Get resource 2 use right"); try { Thread.Sleep (500);} catch (Exception e) {}synchronized (Resource1) {System.out.println ("Thread 2: Waiting for resource 1");}}}; T1.start (); T2.start ();}}
The program will first print out
Thread 1: Get resource 1 Right-of-use thread 2: Get resource 2 right of use
or a
Thread 2: Get resource 2 right-of-use thread 1: Get resource 1 right of use
And then the program doesn't run down.
Thread T1 to "Resource 1" and wait for "Resource 2". While thread T2 gets the "resource 2" use right, wait for "Resource 1". In this way, thread t1 and thread T2 cause a deadlock.
Assuming that the program is Thread.Sleep (500), this line of code stares, the program may cause deadlocks or may not be caused.
Java Deadlock Example