我是这样写的死锁- public class Test1 {
- public static void main(String[] args) {
- DeadLock dl1 = new DeadLock(true);
- DeadLock dl2 = new DeadLock(false);
- new Thread(dl1).start();
- new Thread(dl2).start();
- }
- }
- class DeadLock implements Runnable{
- private boolean flag;
- DeadLock(boolean flag){
- this.flag = flag;
- }
- public void run(){
- if(flag){
- synchronized(LockA.locka){
- System.out.println("if...locka");
- synchronized(LockB.lockb){
- System.out.println("if...lockb");
- }
- }
- }else{
- synchronized(LockB.lockb){
- System.out.println("else...lockb");
- synchronized(LockA.locka){
- System.out.println("else...locka");
- }
- }
- }
- }
- }
- class LockA{
- public static final LockA locka = new LockA();
- }
- class LockB{
- public static final LockB lockb = new LockB();
- }
复制代码
|