Skip to content

Instantly share code, notes, and snippets.

@namkyu
Created June 25, 2018 06:46
Show Gist options
  • Save namkyu/59442b43e1151797a3aea9e625371fa4 to your computer and use it in GitHub Desktop.
Save namkyu/59442b43e1151797a3aea9e625371fa4 to your computer and use it in GitHub Desktop.
public class DeadLockTest {
public void method1() {
String name = new Thread().getName();
synchronized (String.class) {
System.out.println("[" + new Thread().getName() + "] Aquired lock on String.class object in the method1");
try { Thread.sleep(100); } catch (InterruptedException e) {}
synchronized (Integer.class) {
System.out.println("[" + new Thread().getName() + "] Aquired lock on Integer.class object in the method1");
}
}
}
public void method2() {
String name = new Thread().getName();
synchronized (Integer.class) {
System.out.println("[" + new Thread().getName() + "] Aquired lock on Integer.class object in the method2");
try { Thread.sleep(100); } catch (InterruptedException e) {}
synchronized (String.class) {
System.out.println("[" + new Thread().getName() + "] Aquired lock on String.class object in the method2");
}
}
}
public static void main(String[] args) {
DeadLockTest deadLockTest = new DeadLockTest();
new Thread(() -> deadLockTest.method1()).start();
new Thread(() -> deadLockTest.method2()).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment