Java Implementation of the deadlock sample code, java sample code

Source: Internet
Author: User

Java Implementation of the deadlock sample code, java sample code

What is deadlock?

Let's take a look at an example in our daily life: there is a bridge on a river, and the bridge deck is narrow. We can only accommodate one car, so we cannot let the two cars run in parallel. If two vehicles A and B drive on the bridge respectively at both ends of the bridge, then for car A, it goes through A section on the left of the bridge deck (that is, it occupies part of the resources of the bridge ), if you want to bridge the bridge, you still have to wait for B to get out of the Right bridge deck. At this time, car A cannot move forward. For Car B, it goes through the right section of the bridge deck (that is, it occupies part of the bridge resources ), if you want to bridge the bridge, you still have to wait for car A to make way out of the bridge deck on the left. At this time, Car B cannot move forward. The two sides of the car do not reverse, resulting in mutual waiting for the other side to let out of the bridge, but no one will give way, it will wait endlessly. This is a deadlock. If the vehicle ratio is used as A process and the bridge deck is used as A resource, the above problem is described as follows: process A occupies the resource R1, and wait for process B to occupy the resource Rr; process B occupies the resource Rr, wait for resource R1 occupied by process. Resources R1 and Rr can only be occupied by one process, that is, they cannot be occupied by both processes. As a result, neither process can continue to run. If other measures are not taken, the loop wait state will continue indefinitely, resulting in a process deadlock.

In computer systems, deadlocks may occur on software and hardware resources. For example, the system only has one CD-ROM drive and a printer, a process occupies the CD-ROM drive, and apply for a printer; another process occupies the printer, also apply for CD-ROM. As a result, both processes are blocked and cannot be released on their own.

The so-called deadlock refers to the situation where multiple processes are stuck indefinitely while waiting for resources occupied by them. Obviously, without external force, all processes involved in the deadlock will always be blocked. From the above example, we can see that the root cause of the computer system deadlock is limited resources and improper operations. That is, the system provides too few resources to meet the resource requirements of concurrent processes. The deadlock caused by such competitive resources is the core of our discussion. For example, a message is a temporary resource. At A certain time point, process A waits for the message sent by process B, process B waits for the message sent by process C, and process C waits for the message sent by process. When the message is not reached, the processes A, B, and C cannot push forward, and deadlock occurs in Process Communication. Another reason is the deadlock caused by improper process promotion sequence. A deadlock may not occur if there are few resources. Just as if two people crossed the zhuqiao bridge, if both of them had to go over first, and the two were deadlocked and refused to retreat on the zhuqiao, they would inevitably have to compete for resources to create deadlocks. However, if two people first see whether or not they are on the bridge and they are on the bridge when they are on the bridge, the problem will be solved. Therefore, if the program is not properly designed, the process may be pushed in an improper order, and deadlocks may also occur.

Deadlock

The deadlock occurs only when t1 thread occupies o1 and T2. t2 occupies o2 at this time and o1 is required. (similar to eating with two chopsticks, they all need a pair of chopsticks to eat)

In the following code, t1 occupies o1, and o1 is released only after the o2 object is obtained. t2 occupies o2 and obtains o1 first, and o1 is occupied by t1 thread at this time, o2 is occupied by T2. t1 and T2. there will be deadlocks.

Package implements imple;/*** deadlock demo * @ author haokui **/public class DieSynchronized {public static void main (String [] args) {/*** create and start two threads t1 and t2. Both threads must share both o1 and o2 objects */Object o1 = new Object (); Object o2 = new Object (); Thread t1 = new Thread (new T1 (o1, o2); Thread t2 = new Thread (new T2 (o1, o2); t1.start (); t2.start ();}} // create two thread classes class T1 implements Runnable {Object o1; Object o2; public T1 (Object o1, Object o2) {this. o1 = o1; this. o2 = o2;} public void run () {// lock o1 and o2 synchronized (o1) {try {Thread. sleep (1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} synchronized (o2) {System. out. println ("o2") ;}}} class T2 implements Runnable {Object o1; Object o2; public T2 (Object o1, Object o2) {this. o1 = o1; this. o2 = o2;} public void run () {synchronized (o2) {try {Thread. sleep (1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} synchronized (o1) {System. out. println ("o1 ");}}}}

Note: concurrency occurs only when o1 and o2 are shared. You can share two objects by using constructors.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.