模拟简单死锁

public class T3 {

	public static void main(String[] args) {

		final Object o1 = new String[] { "t1" };
		final Object o2 = new String[] { "t2" };

		new Thread(new Runnable() {

			public void run() {
				DeadLock.test(o1, o2);
			}
		}).start();
		new Thread(new Runnable() {

			public void run() {
				DeadLock.test(o2, o1);
			}
		}).start();
	}
}

class DeadLock {

	public static void test(Object o1, Object o2) {
		synchronized (o1) {
			System.err.println("get lock of: " + o1.toString());
			try {
				TimeUnit.SECONDS.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (o2) {
				System.err.println("get lock of: " + o2.toString());
			}
		}
	}

}
/**
** console output
** get lock of: [Ljava.lang.String;@64acfc45
** get lock of: [Ljava.lang.String;@6f0f2a6f
**/

你可能感兴趣的:(模拟简单死锁)