Created
June 25, 2018 06:46
-
-
Save namkyu/59442b43e1151797a3aea9e625371fa4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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