SHARE
TWEET

Untitled

a guest May 21st, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package threads;
  2. import java.util.concurrent.CountDownLatch;
  3. public class Deadlock {
  4.     static CountDownLatch latch = new CountDownLatch(1);
  5.     public synchronized void method1() {
  6.         System.out.println("Method1");
  7.     }
  8.     public synchronized void method2() {
  9.         System.out.println("Method2");
  10.     }
  11.     public static void main(String[] args) {
  12.         Deadlock d1 = new Deadlock();
  13.         Deadlock d2 = new Deadlock();
  14.         Thread t1 = new Thread(new Runnable() {
  15.             public void run() {
  16.                 try {
  17.                     latch.await();
  18.                 } catch (InterruptedException e) {
  19.                     // TODO Auto-generated catch block
  20.                     e.printStackTrace();
  21.                 }
  22.                 d1.method1();
  23.                 d2.method2();
  24.             }
  25.         });
  26.         Thread t2 = new Thread(new Runnable() {
  27.             public void run() {
  28.                 try {
  29.                     latch.await();
  30.                 } catch (InterruptedException e) {
  31.                     // TODO Auto-generated catch block
  32.                     e.printStackTrace();
  33.                 }
  34.                 d2.method2();
  35.                 d1.method1();
  36.             }
  37.         });
  38.         t1.start();
  39.         t2.start();
  40.         latch.countDown();
  41.     }
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top