Implementation of deadlock in java Multithreading
1. directly add the Code:
Class LockDemo {public static final Object A_LOCK = new Object (); public static final Object B _LOCK = new Object ();} public class ThreadLockDemo implements Runnable {public boolean flag; public ThreadLockDemo (boolean flag) {this. flag = flag;} public void run () {if (flag) {synchronized (LockDemo. a_LOCK) {System. out. println ("if A_LOCK"); synchronized (LockDemo. B _LOCK) {System. out. println ("B _LOCK") ;}} else {synchronized (LockDemo. B _LOCK) {System. out. println ("else B _LOCK"); synchronized (LockDemo. a_LOCK) {System. out. println ("A_LOCK") ;}}} public static void main (String [] args) {// TODO Auto-generated method stubThreadLockDemo td1 = new ThreadLockDemo (false ); threadLockDemo td2 = new ThreadLockDemo (true); Thread t1 = new Thread (td1); Thread t2 = new Thread (td2); t1.start (); t2.start ();}}
2. Have a picture and have a truth