經典的哲學家就餐問題,還有印象嗎?
哲學家就餐問題(Dining Philosophers Problem)是由計算機科學家艾茲格?W?迪科斯徹(Edsger W. Dijkstra)于 1965 年提出的一個經典的多線程同步問題,用于描述和解決并發編程中的資源分配與死鎖問題。
在本文中,我們探討如何使用Java解決這一問題。
一、問題描述
想象有一張圓桌,周圍坐著五位哲學家。每位哲學家的面前都有一碗食物(如意大利面)和兩根筷子(分別位于其左右兩側)。
哲學家們的生活只有兩種狀態:思考和吃飯。要吃飯的話,哲學家必須同時拿起他左邊和右邊的筷子,吃完后放下筷子繼續思考。
由于筷子數量有限,只有五根,且每位哲學家都需要兩根筷子才能進餐,這就可能導致資源競爭和死鎖的情況發生。
例如,每個哲學家都先拿起了左邊的筷子,然后等待右邊的筷子,此時所有哲學家都在等待其他哲學家放下筷子,而又都不會放下自己已經拿到的筷子,這樣就造成了死鎖,整個系統陷入僵持狀態,沒有哲學家能夠吃到食物。
二、解決辦法
為了解決這個問題,需要設計一種合理的資源分配策略,確保哲學家們能夠有序地獲取和釋放筷子,避免死鎖的發生。常見的解決方案包括:
- 資源分級:給筷子編號,規定哲學家先拿起編號小的筷子,再拿起編號大的筷子,這樣可以避免循環等待。
- 奇數哲學家先拿左邊筷子,偶數哲學家先拿右邊筷子:打破循環等待的條件,使得至少有一位哲學家能夠獲取到兩根筷子開始進餐,從而打破死鎖局面。
- 使用并發控制機制:如信號量、互斥鎖等,來控制哲學家對筷子的訪問,確保資源的合理分配和同步。
三、使用同步塊解決哲學家就餐問題
解決哲學家就餐問題的一種方法是使用Java的同步塊。
我們可以為每根筷子創建一個對象,將其作為鎖。哲學家線程在嘗試拿起筷子時會獲取相應的鎖。
以下是實現代碼:
public class DiningPhilosophersSynchronized {
public static void main(String[] args) {
Object[] chopsticks = new Object[5];
for (int i = 0; i < 5; i++) {
chopsticks[i] = new Object();
}
Thread[] philosophers = new Thread[5];
for (int i = 0; i < 5; i++) {
int leftChopstick = i;
int rightChopstick = (i + 1) % 5;
philosophers[i] = new Thread(new Philosopher(chopsticks[leftChopstick], chopsticks[rightChopstick]));
philosophers[i].setName("Philosopher " + (i + 1));
philosophers[i].start();
}
}
static class Philosopher implements Runnable {
private final Object leftChopstick;
private final Object rightChopstick;
public Philosopher(Object leftChopstick, Object rightChopstick) {
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}
@Override
public void run() {
try {
while (true) {
System.out.println(Thread.currentThread().getName() + " is thinking");
Thread.sleep((long) (Math.random() * 1000));
System.out.println(Thread.currentThread().getName() + " is hungry");
synchronized (leftChopstick) {
System.out.println(Thread.currentThread().getName() + " picked up left chopstick");
synchronized (rightChopstick) {
System.out.println(Thread.currentThread().getName() + " picked up right chopstick");
System.out.println(Thread.currentThread().getName() + " is eating");
Thread.sleep((long) (Math.random() * 1000));
System.out.println(Thread.currentThread().getName() + " put down right chopstick");
}
System.out.println(Thread.currentThread().getName() + " put down left chopstick");
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
在上述代碼中:
- 我們創建了一個包含五個對象的數組chopsticks,每個對象代表一根筷子。
- 為每個哲學家創建一個線程。每個哲學家線程嘗試獲取左邊和右邊筷子的鎖,以模擬拿起筷子吃飯的過程。
- 在Philosopher類的run方法中,哲學家首先思考一段時間,然后進入饑餓狀態,嘗試獲取兩根筷子。如果成功獲取兩根筷子,就開始吃飯,吃完后放下筷子繼續思考。
運行結果為:
Philosopher 3 is thinking
Philosopher 5 is thinking
Philosopher 4 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 4 is hungry
Philosopher 4 picked up left chopstick
Philosopher 4 picked up right chopstick
Philosopher 4 is eating
Philosopher 3 is hungry
Philosopher 3 picked up left chopstick
Philosopher 4 put down right chopstick
Philosopher 4 put down left chopstick
Philosopher 3 picked up right chopstick
Philosopher 3 is eating
Philosopher 4 is thinking
Philosopher 1 is hungry
Philosopher 1 picked up left chopstick
……
四、使用 ReentrantLock 解決哲學家就餐問題
除了同步塊,我們還可以使用ReentrantLock來解決哲學家就餐問題。ReentrantLock提供了比同步塊更靈活的鎖機制,例如可中斷的鎖獲取、公平性選擇等。
以下是使用ReentrantLock的實現:
import java.util.concurrent.locks.ReentrantLock;
public class DiningPhilosophersReentrantLock {
public static void main(String[] args) {
ReentrantLock[] chopsticks = new ReentrantLock[5];
for (int i = 0; i < 5; i++) {
chopsticks[i] = new ReentrantLock();
}
Thread[] philosophers = new Thread[5];
for (int i = 0; i < 5; i++) {
int leftChopstick = i;
int rightChopstick = (i + 1) % 5;
philosophers[i] = new Thread(new Philosopher(chopsticks[leftChopstick], chopsticks[rightChopstick]));
philosophers[i].setName("Philosopher " + (i + 1));
philosophers[i].start();
}
}
static class Philosopher implements Runnable {
private final ReentrantLock leftChopstick;
private final ReentrantLock rightChopstick;
public Philosopher(ReentrantLock leftChopstick, ReentrantLock rightChopstick) {
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}
@Override
public void run() {
try {
while (true) {
System.out.println(Thread.currentThread().getName() + " is thinking");
Thread.sleep((long) (Math.random() * 1000));
System.out.println(Thread.currentThread().getName() + " is hungry");
boolean leftAcquired = false;
boolean rightAcquired = false;
try {
leftAcquired = leftChopstick.tryLock();
if (leftAcquired) {
System.out.println(Thread.currentThread().getName() + " picked up left chopstick");
rightAcquired = rightChopstick.tryLock();
if (rightAcquired) {
System.out.println(Thread.currentThread().getName() + " picked up right chopstick");
System.out.println(Thread.currentThread().getName() + " is eating");
Thread.sleep((long) (Math.random() * 1000));
}
}
} finally {
if (rightAcquired) {
rightChopstick.unlock();
System.out.println(Thread.currentThread().getName() + " put down right chopstick");
}
if (leftAcquired) {
leftChopstick.unlock();
System.out.println(Thread.currentThread().getName() + " put down left chopstick");
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
在這個實現中:
- 我們創建了一個ReentrantLock數組來表示筷子。
- 在Philosopher類的run方法中,哲學家嘗試使用tryLock方法獲取左邊和右邊筷子的鎖。tryLock方法會嘗試獲取鎖,如果鎖不可用,它會立即返回false,而不會阻塞線程。
- 如果成功獲取兩根筷子,哲學家開始吃飯,吃完后釋放鎖。這種方式可以避免死鎖,因為如果無法獲取兩根筷子,哲學家會釋放已經獲取的筷子,從而避免一直持有鎖導致其他哲學家無法獲取資源。
運行結果為:
Philosopher 1 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 2 is thinking
Philosopher 5 is thinking
Philosopher 4 is hungry
Philosopher 4 picked up left chopstick
Philosopher 4 picked up right chopstick
Philosopher 4 is eating
Philosopher 2 is hungry
Philosopher 2 picked up left chopstick
Philosopher 2 picked up right chopstick
Philosopher 2 is eating
Philosopher 5 is hungry
Philosopher 5 is thinking
Philosopher 4 put down right chopstick
……
五、死鎖預防策略
為了避免死鎖,我們可以采用以下幾種策略:
- 資源分配圖算法:使用資源分配圖算法(如銀行家算法)來檢測和避免死鎖。該算法通過監控資源的分配和請求情況,確保系統始終處于安全狀態,即不會發生死鎖。
- 破壞死鎖的四個必要條件:死鎖的發生需要滿足四個必要條件:互斥、占有并等待、不可剝奪和循環等待。我們可以通過破壞其中一個或多個條件來預防死鎖。例如,在哲學家就餐問題中,我們可以通過改變資源分配方式來破壞循環等待條件,比如讓一位哲學家先拿起右邊的筷子,再拿起左邊的筷子,這樣就打破了循環等待的結構。
- 超時機制:在獲取鎖時設置超時時間。如果在指定時間內無法獲取所有需要的資源,線程可以放棄已獲取的資源,并重新嘗試獲取,從而避免無限期等待導致的死鎖。在上述ReentrantLock的實現中,tryLock方法就是一種簡單的超時機制體現,它嘗試獲取鎖,如果在調用時鎖不可用,會立即返回false,而不是一直阻塞等待。
文末總結
在本文中,我們詳細探討了Java中哲學家就餐問題的解決方案,包括使用同步塊和ReentrantLock。同時,我們也討論了死鎖預防的策略。通過這些方法和策略,我們能夠有效地處理多線程環境下的資源競爭和死鎖問題,提高程序的并發性能和穩定性。