Advertisement
Guest User

Untitled

a guest
May 21st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement