DeadLock.md 1001 字节
Newer Older
CSDN-Ada助手's avatar
CSDN-Ada助手 已提交
1 2 3
# DeadLock

两个线程分别运行以下代码的eatDrink和drinkEat, 会不会发生死锁:
M
Mars Liu 已提交
4

CSDN-Ada助手's avatar
CSDN-Ada助手 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
```java
public class DeadLock {
    private final String eat = "吃饭";
    private final String drink = "喝水";

    public void eatDrink() {
        synchronized (eat) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (drink) {
                // doSomething
            }
        }
    }

    public void drinkEat() {
        synchronized (drink) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (eat) {
                // doSomething
            }
        }
    }
}
```



## 答案

```

```

## 选项

### A

```
不会
```
M
Mars Liu 已提交
53 54 55 56 57 58 59 60 61 62 63 64

### B

```
只有在多核环境下才会死锁
```

### C

```
只有多核环境下才不会死锁
```