1 package deadlock; 20 21 24 public class Deadlock implements Runnable { 25 28 String name; 29 30 33 Deadlock other; 34 35 public Deadlock (String name) { 36 this.name = name; 37 } 38 39 public static void main (String [] args) { 40 Deadlock o1 = new Deadlock("A"); 41 Deadlock o2 = new Deadlock("B"); 42 43 o1.other = o2; 44 o2.other = o1; 45 46 Thread t1 = new Thread (o1); 47 Thread t2 = new Thread (o2); 48 49 t1.start(); 50 t2.start(); 51 } 52 53 public void run () { 54 while (true) { 55 System.out.println(name + " cycle start"); 56 synchronized (this) { 57 other.foo(); 58 } 59 60 System.out.println(name + " cycle end"); 61 } 62 } 63 64 65 synchronized void foo () { 66 System.out.println(name + ".foo() was called"); 67 } 68 } | Popular Tags |