The history of super-multi-threaded deadlock

Source: Internet
Author: User


* Synchronized: Not to the object inside the yoke, the lock is a property of each object, synchronized just locked the lock, is a continuous action
* A deadlock is a condition that everyone can't access.
 *
* Two threads,
* Two threads each access two mutually exclusive objects
 *
* Principle:
* 1 A thread execution completes, a nested lock is required to execute two objects, but in the process, the first object must be locked
* 2 Another thread execution completes, also need to have nested locking to execute two objects, first to perform a lock on the second image
* 3 During the first execution, when the second object is executed, the discovery is locked, waiting for
* 4 During the execution of the second thread, when the first object is executed, it is found that the first one is locked and can only wait.

Public class Thread deadlock{
Public static void Main (string[] args) {
Object O1 = new Object ();
Object O2 = new Object ();

thread T1 = new Thread (new A (O1,O2));
thread t2 = new Thread (new B (O1,O2));
T1.start ();
T2.start ();
    }
}

class A implements runnable{
Object O1;
Object O2;

T1 (Object O1, Object O2) {
this.o1 = O1;
this.o2 = O2;
    }

Public void Run () {
synchronized (O1) {
try{
Thread.Sleep (+);
}catch (Exception e) {
e.printstacktrace ();
            }
synchronized (O2) {
System.out.println ("T1");
            }
        }
    }
}

class B implements runnable{
Object O1;
Object O2;

T2 (Object O1, Object O2) {
this.o1 = O1;
this.o2 = O2;
    }

Public void Run () {
synchronized (O2) {
try{
Thread.Sleep (+);
}catch (Exception e) {
e.printstacktrace ();
            }
synchronized (O1) {
System.out.println ("T2");
            }
        }
    }
}

The history of super-multi-threaded deadlock

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.