Java Tread multithreading (3) deadlock and tread Multithreading
Author: Qing Dujun
Address: http://blog.csdn.net/qingdujun/article/details/39367655
This article demonstrates the Tread multi-thread deadlock. It briefly analyzes the cause of the deadlock and shows a small Demo of the deadlock.
1) cause of deadlock
2) Demo
I. Cause of deadlock: Nested synchronization in synchronization.
II. A small deadlock Demo: (lockb needs to be locked in the lock loacka, locka needs to be locked in the lock lockb, and then while (true) is added in the inside, and the lock is directly locked .)
Package no. zuo. no. die; // 1. implement the Runnable interface class Zuosi implements Runnable {private boolean flag; Zuosi (boolean f) {flag = f ;}// 2. override the run method public void run () {if (flag) {while (true) {synchronized (CLock. locka) {System. out. println ("if locka"); synchronized (CLock. lockb) {System. out. println ("if lockb") ;}}} else {while (true) {synchronized (CLock. lockb) {System. out. println ("else lockb"); synchronized (CLock. locka) {System. out. println ("else locka") ;}}}// contains 2 locks class CLock {static Object locka = new Object (); static Object lockb = new Object ();} public class CDeadLock {public static void main (String [] args) {// 3. create 2 threads Thread t1 = new Thread (new Zuosi (true); Thread t2 = new Thread (new Zuosi (false); // 4. start thread t1.start (); t2.start ();}}
Running result (stuck? Locked !!!) :
References: Lecture on Java video Bi Xiangdong
Address: http://blog.csdn.net/qingdujun/article/details/39367655
JAVA multi-thread resource sharing and deadlock
Thread thread1 = new Thread (new Test (true ));
The flag in the object is true; Lock. lock1 is obtained.
,
Thread thread2 = new Thread (new Test (false ));
The flag in the object is false, and the Lock. lock2 is obtained.
,
Then thread1 wants to get Lock. lock2,
Code:
Synchronized (Lock. lock2 ){
System. out. println ("if-lock2 ");
}
Then thread2 wants to get Lock. lock1,
Code: synchronized (Lock. lock1 ){
System. out. println ("else-lock1 ");
}
In this way, they waited for each other and formed a deadlock.
Java multi-thread deadlock
The Code has been modified, and problems may occur. (1) The sleep time is too short, thread 2 has not been started, and tickets have been sold out (2) Two while are endless loops, tickets are sold out, but they are not exited. (3) the cause of the deadlock is suspected to be the conflict between the two Obj locks. It may be that the two threads each get one lock and there is no unlocking mechanism, so I deleted the obj lock class Ticket implements Runnable {Object obj = new Object (); boolean flag = true; {while (tick> 0) {synchronized (obj) in the show method) // obj lock {show () ;}} else {while (tick> 0) show () ;}} public synchronized void show () {// this lock if (tick> 0) {try {Thread. sleep (100);} catch (Exception e) {} System. out. println (Thread. currentThread (). getName () + "code .........: "+ tick --) ;}} public class DeadLockDemo {public static void main (String [] args) {Ticket t = new Ticket (); thread t1 = new Thread (t); Thread t2 = new Thread (t); t1.start (); try {Thread. sleep (10);} catch (Exception e) {} t. flag = false; t2.start ();}}