PackageCom.swift; Public classDeadLockImplementsRunnable {Private BooleanFlag; DeadLock (Booleanflag) { This. Flag =Flag; } Public voidrun () { while(true) { if(flag) {synchronized("Suo1") {System.out.println (Thread.CurrentThread (). GetName ()+ "If....locka"); synchronized("Suo2") {System.out.println (Thread.CurrentThread (). GetName ()+ "IF.....LOCKB"); } } } Else { synchronized("Suo2") {System.out.println (Thread.CurrentThread (). GetName ()+ "ELSE.....LOCKB"); synchronized("Suo1") {System.out.println (Thread.CurrentThread (). GetName ()+ "Else....locka"); } } } } } Public Static voidMain (string[] args) {/** Write a deadlock*/ //It's only when we wait for each other to give up resources to create deadlocks . NewThread (NewDeadLock (true), "Thread 1"). Start (); NewThread (NewDeadLock (false), "Thread 2"). Start (); }}
The lock of the synchronization code block can also be used with objects, such as Locka.locka
Locka object is static public
Java threading problem, write a deadlock (principle: Only each other waits for the other person to abandon the resources to produce a deadlock)