⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fixedphilosopher.java

📁 英国帝国理工学院电脑系多线程课程教学材料之九 ---- 哲学家晚餐范例(无死锁
💻 JAVA
字号:
package concurrency.diners;import java.awt.*;class FixedPhilosopher extends Thread {    int identity;    boolean stopRequested = false;    PhilCanvas view;    Diners controller;    Fork left;    Fork right;    FixedPhilosopher(Diners controller, int identity, Fork left, Fork right) {        this.controller = controller;        this.view = controller.display;        this.identity = identity;        this.left = left;        this.right = right;    }    public void run() {        while (!stopRequested) {             try {                //thinking                view.setPhil(identity,view.THINKING);                sleep(controller.sleepTime());                //hungry                view.setPhil(identity,view.HUNGRY);                //get forks                if (identity%2 == 0) {                    left.get();                    view.setPhil(identity,view.GOTLEFT);                } else {                    right.get();                    view.setPhil(identity,view.GOTRIGHT);                }                sleep(500);                if (identity%2 == 0)                    right.get();                else                    left.get();                //eating                view.setPhil(identity,view.EATING);                sleep(controller.eatTime());                right.put();                left.put();             } catch (java.lang.InterruptedException e) {}        }    }    public void stopRequested() {        stopRequested = true;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -