* 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