Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package threads;
- import java.util.concurrent.CountDownLatch;
- public class Deadlock {
- static CountDownLatch latch = new CountDownLatch(1);
- public synchronized void method1() {
- System.out.println("Method1");
- }
- public synchronized void method2() {
- System.out.println("Method2");
- }
- public static void main(String[] args) {
- Deadlock d1 = new Deadlock();
- Deadlock d2 = new Deadlock();
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- try {
- latch.await();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- d1.method1();
- d2.method2();
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- try {
- latch.await();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- d2.method2();
- d1.method1();
- }
- });
- t1.start();
- t2.start();
- latch.countDown();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement