Thread Sync #2. 實作篇 – 互相等待的兩個執行緒

Thread Sync #2. 實作篇 – 互相等待的兩個執行緒

繼上篇,有人跟我講太深奧了… Orz, 其實不會,只是還沒看到 Code 而以…。就先來幫黑暗魔人賽說明一下程式碼…。首先來看的是黑暗大魔王: GameHost..

GameHost 呼叫 Player 的片段

1: public void Start(Player p)

2: {

3: // 略…

4: int[] guess = p.StartGuess(_maxNum, _digits);

5: // 略…

6: Hint hint = compare(guess);

7: // 略…

8: while (hint.A != _digits)

9: {

10: // 略…

11: guess = p.GuessNext(hint);

12: // 略…

13: hint = compare(guess);

14: }

15: p.Stop();

16: // 略…

17: }

這段程式完全是老闆的角度在思考。抓到 PLAYER 後就叫它開始猜 StartGuess(),然後拼命的叫 PLAYER 再猜 GuessNext(), 直到猜中才可以休息 Stop()

很典型的多型 ( Polymorphism ) 應用,實際上會 RUN 什麼 CODE,就看繼承 PLAYER 的人是怎麼寫的…。這次我們再從弱勢勞工的角度來看看 PLAYER 該怎麼實作 (以 darkthread 附的 DummyPlayer 為例):

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

What is 7 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)