1 package nsu.fit.javaperf; 2 3 /** 4 * Deadlock showcase. 5 * 6 * Usage: java -cp out/testgcpause.jar nsu.fit.javaperf.TestDeadLock 7 */ 8 public class TestDeadLock { 9 10 static final Object lock1 = new Object(); 11 12 static final Object lock2 = new Object(); 13 14 public static void main(String[] args) throws Exception { 15 16 new Thread(new Runnable() { 17 public void run() { 18 synchronized (lock2) { //Lock2 19 System.err.println("t1: lock2"); 20 try { 21 System.err.println("t1: lock2.wait()"); 22 lock2.wait(); 23 } catch (InterruptedException e) { 24 throw new RuntimeException(e); 25 } 26 synchronized (lock1) { //Lock1 27 System.err.println("t1: lock1"); 28 } 29 } 30 } 31 }).start(); 32 33 Thread.sleep(100); 34 35 new Thread(new Runnable() { 36 public void run() { 37 synchronized (lock1) { 38 System.err.println("t2: lock1"); 39 synchronized (lock2) { 40 System.err.println("t2: lock2.notify"); 41 lock2.notify(); 42 } 43 try { 44 Thread.sleep(100); 45 } catch (InterruptedException e) { 46 throw new RuntimeException(e); 47 } 48 synchronized (lock2) { 49 System.err.println("t2: lock2"); 50 } 51 } 52 } 53 }).start(); 54 } 55 } 56