/**
* java_死锁问题_用最简单的代码重现死锁问题
*
*/
package 重要练习0922;
import java.util.ArrayList;
public class 死锁 {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<>();
ArrayList<String> 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();
}
}
java多线程_死锁问题_用最简单的代码重现死锁问题
最新推荐文章于 2024-04-21 03:22:05 发布