模拟死锁
作者:互联网
public class MyClass { private static Object resourceA = new Object(); private static Object resourceB = new Object(); public static void main(String args[]) { Thread threadA = new Thread( new Runnable() { public void run() { System.out.println(Thread.currentThread() + " getting A"); synchronized (resourceA) { System.out.println(Thread.currentThread() + " got A"); try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread() + " getting B"); synchronized (resourceB) { System.out.println(Thread.currentThread() + " got B"); try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } } } } } ); Thread threadB = new Thread( new Runnable() { public void run() { System.out.println(Thread.currentThread() + " getting B"); synchronized (resourceB) { System.out.println(Thread.currentThread() + " got B"); try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread() + " getting A"); synchronized (resourceA) { System.out.println(Thread.currentThread() + " got A"); try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } } } } } ); threadA.start(); threadB.start(); } }
标签:Thread,currentThread,System,死锁,println,new,模拟,out 来源: https://www.cnblogs.com/silva/p/15705588.html