Examples of deadlocks

Source: Internet
Author: User

Examples of deadlocks

Package Day11_second;public class Deathlock {public static void main (string[] args) {//TODO auto-generated method Stubdea ThLocked1 DL1 = new DeathLocked1 ();D eathLocked2 DL2 = new DeathLocked2 (); thread T1 = new Thread (DL1); Thread t2 = new Thread (DL2); T1.start (); T2.start ();}} Class Res {public static object obj1 = new Object ();p ublic static Object obj2 = new Object ();} Class DeathLocked1 implements runnable{@Overridepublic void Run () {//TODO auto-generated method Stubfun ();} private void Fun () {//TODO auto-generated method stubsynchronized (res.obj1) {try {thread.sleep (+);} catch (Interrupte Dexception e) {e.printstacktrace ();} Synchronized (RES.OBJ2) {System.out.println ("Call Obj2 in Obj1");}}} Class DeathLocked2 implements runnable{@Overridepublic void Run () {//TODO auto-generated method Stubfun ();} private void Fun () {//TODO auto-generated method stubsynchronized (res.obj2) {try {thread.sleep (+);} catch (Interrupte Dexception e) {e.printstacktrace ();} Synchronized (res.obj1) {System.out. println ("Call obj1 in Obj2");}}} 

After starting the thread T1, execute the T1 fun method, occupy the O1 resource, and then T1 hibernate to make sure that T2 can be executed. T2 executes the fun () method, occupying O2 resources. This is the fourth necessary condition for a deadlock. That is, the thread T1 occupies the necessary resources of T2, T2 occupies the necessary resources of T1, both sides do not release, that is, the formation of deadlock.

http://my.oschina.net/u/1469592/blog/208374

Examples of deadlocks

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.