Skip to content

Instantly share code, notes, and snippets.

@Rhtyme
Created November 27, 2020 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rhtyme/ec91195d98aee16f24e78f61bd1970b8 to your computer and use it in GitHub Desktop.
Save Rhtyme/ec91195d98aee16f24e78f61bd1970b8 to your computer and use it in GitHub Desktop.
Deadlock example in Java
public class DeadLockThread {
public static Object object1 = new Object();
public static Object object2 = new Object();
public static void main(String args[]) {
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
thread1.start();
thread2.start();
}
private static class Thread1 extends Thread {
public void run() {
synchronized (object1) {
System.out.println("Thread 1: Holds object 1.");
try {
Thread.sleep(10);
} catch (Exception e) {}
System.out.println("Thread 1: Waiting for object 2.");
synchronized (object2) {
System.out.println("Thread 1: Holds object 1 and object 2.");
}
}
}
}
private static class Thread2 extends Thread {
public void run() {
synchronized (object2) {
System.out.println("Thread 2: Holds object 2.");
try {
Thread.sleep(10);
} catch (Exception e) {}
System.out.println("Thread 2: Waiting for object 1.");
synchronized (object1) {
System.out.println("Thread 2: Holds object 1 and object 2.");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment