Write a Java deadlock demo

Source: Internet
Author: User

Package Com.simon.study;/** thread deadlock A thread must have two objects at the same time for the next operation; * @author: Simon * @date: July 29, 2017 pm 9:48:43*/ Public classDeadLock implements runnable{StaticObject o1=NewObject (), o2=NewObject ();  Public intflag=1;  Public voidrun () {//TODO auto-generated Method StubSystem. out. println ("flag***********"+flag); if(flag==1) {synchronized (O1) {Try{Thread.Sleep ( -); } Catch(Exception e) {//Todo:handle Exception} synchronized (O2) {System. out. println ("here thread 1 gets all the thread permissions"); }                }                                }           if(flag==0) {synchronized (O2) {Try{Thread.Sleep ( -); } Catch(Exception e) {//Todo:handle Exception} synchronized (O1) {System. out. println ("here thread 2 gets all the thread permissions"); }                }                           }           }      Public Static voidMain (string[] args) {DeadLock deadLock1=NewDeadLock (); DeadLock DeadLock2=NewDeadLock (); Deadlock1.flag=1; Deadlock2.flag=0; Thread R1=NewThread (DEADLOCK1); Thread R2=NewThread (DEADLOCK2);           R1.start ();     R2.start (); }     } 

Write a Java deadlock demo

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.