deadlocks are generally caused by mutual lock wait between two objects.
The following example is used to construct this situation:
Package basic. e_deadlock; import Org. apache. log4j. logger; public class testdeadlock {public static void main (string [] ARGs) {deadlockrisk dead = new deadlockrisk (); mythread T1 = new mythread (dead, 1, 2, "thread 1"); mythread t2 = new mythread (dead, 3, 4, "thread 2"); mythread T3 = new mythread (dead, 5, 6, "thread 3"); mythread t4 = new mythread (dead, 7, 8, "thread 4"); t1.start (); t2.start (); t3.start (); t4.start () ;}} class MYT Hread extends thread {private deadlockrisk dead; private int A, B; mythread (deadlockrisk dead, int A, int B, string threadname) {This. dead = dead; this. A = A; this. B = B; this. setname (threadname) ;}@ overridepublic void run () {dead. read (); dead. write (a, B) ;}} class deadlockrisk {Private Static logger = logger. getlogger (deadlockrisk. class); Private Static class resource {public int value;} private reso Urce resourcea = new resource (); private resource resourceb = new resource (); Public void read () {logger. debug ("============= read begin ============"); synchronized (resourcea) {logger. debug ("Read ():" + thread. currentthread (). getname () + "gets the resourcea lock! "); Synchronized (resourceb) {logger. debug (" Read (): "+ thread. currentthread (). getname () +" gets the resourceb lock! ") ;}} Logger. debug ("============= read end ==============");} public void write (int, int B) {logger. debug ("============= write begin ============"); synchronized (resourceb) {logger. debug ("write ():" + thread. currentthread (). getname () + "gets the resourceb lock! "); Synchronized (resourcea) {logger. debug (" write (): "+ thread. currentthread (). getname () +" gets the resourcea lock! "); Resourcea. value = A; resourceb. value = B ;}} logger. debug ("============= write end ============== ");}}
execution result:
0 [thread 1] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 2] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 4] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 3] Debug basic. e_deadlock.deadlockrisk-=========== read begin =========== 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-read (): thread 1 gets the resourcea lock! 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-read (): thread 1 gets the resourceb lock! 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-============= read end ================= 0 [thread 2] Debug basic. e_deadlock.deadlockrisk-read (): thread 2 obtains the resourcea lock! 0 [thread 1] Debug basic. e_deadlock.deadlockrisk-============ write being ============= 1 [thread 2] Debug basic. e_deadlock.deadlockrisk-read (): thread 2 obtains the resourceb lock! 1 [thread 2] Debug basic. e_deadlock.deadlockrisk-============= read end ================= 1 [thread 2] Debug basic. e_deadlock.deadlockrisk-============ write being ============= 1 [thread 1] Debug basic. e_deadlock.deadlockrisk-write (): thread 1 gets the resourceb lock! 1 [thread 4] Debug basic. e_deadlock.deadlockrisk-read (): thread 4 gets the resourcea lock!
Note: thread 1 is waiting for resourceb resources, and thread 2 is waiting for resourcea resources. The two threads are waiting for each other and there is a deadlock.