java多线程_死锁问题_用最简单的代码重现死锁问题

/**
 * java_死锁问题_用最简单的代码重现死锁问题
 * 
 */
package 重要练习0922;

import java.util.ArrayList;

public class 死锁 {

	public static void main(String[] args) {

		ArrayList a = new ArrayList<>();
		ArrayList b = new ArrayList<>();
		// 线程1
		new Thread() {
			public void run() {
				while (true) {
					synchronized (a) {
						System.out.println(Thread.currentThread().getName() + "我拿到了a");
						synchronized (b) {
							System.out.println(Thread.currentThread().getName() + "我拿到了b");
						}
					}
				}

			};
		}.start();
		// 线程2
		new Thread() {
			public void run() {
				while (true) {
					synchronized (b) {
						System.out.println(Thread.currentThread().getName() + "我拿到了b");
						synchronized (a) {
							System.out.println(Thread.currentThread().getName() + "我拿到了a");
						}
					}
				}

			};
		}.start();

	}

}

你可能感兴趣的:(javaSE学习)