Java Deadlock Example

Source: Internet
Author: User

Deadlock:

In the case of multi-threaded competition using shared resources, there is a possibility of deadlocks. For example, when a thread waits for a lock held by another thread, that thread may be waiting for a lock held by the first thread. At this point, the two threads are stuck in an endless wait state, which is called a deadlock.

The four necessary conditions for creating a deadlock:

1, mutually exclusive 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 resources that the process has acquired cannot be stripped until it is exhausted and can only be released by itself when it is exhausted.

4, loop waiting conditions. In the event of a deadlock, there must be a process-a circular chain of resources, that is, the process set {P1,P2, ... , the P1 in Pn} waits for a P2 to occupy the resource, P2 is waiting for a P3 to occupy the resource, ... , PN is waiting for resources that have been consumed by P1.


A relatively simple way to resolve deadlocks is to prevent deadlocks by setting certain constraints to destroy one or more of the four necessary conditions that produce deadlocks. The prevention of deadlocks is a more easily implemented method and has been widely used. However, due to the often restrictive conditions imposed, the system resource utilization and system throughput can be reduced.

Here is an example of 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
Then the program does not go down.

Thread T1 gets "Resource 1" and waits for "Resource 2", while thread T2 gets "Resource 2" and waits for "Resource 1", so that thread t1 and thread T2 cause a deadlock.

If you put Thread.Sleep (500) in the program, this line of code comments, the program may cause deadlocks or may not be caused.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.