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

📄 philosopher.java

📁 北大Java 语言程序设计 ppt课件及源码
💻 JAVA
字号:
/*  * This class requires no changes from the 1.0 version.  * It's kept here so the rest of the example can compile. */import java.util.Random;import java.awt.*;import javax.swing.JLabel;import javax.swing.ImageIcon;public class Philosopher extends JLabel implements Runnable {    private Chopstick leftStick, rightStick;    private boolean sated;    private DiningPhilosophers parent;    private int position;    Thread philThread = null;    public Philosopher(DiningPhilosophers parent, int position, ImageIcon img) {        super(parent.names[position], img, JLabel.CENTER);        this.parent = parent;        this.position = position;        setVerticalTextPosition(JLabel.BOTTOM);        setHorizontalTextPosition(JLabel.CENTER);        // identify the chopsticks to my right and left        this.rightStick = parent.chopsticks[position];        if (position == 0) {            this.leftStick = parent.chopsticks[parent.NUMPHILS-1];        } else {            this.leftStick = parent.chopsticks[position-1];        }        // I'm hungry        this.sated = false;        philThread = new Thread(this);    }    public void run() {        try {            while (true) {	        Thread.sleep((int)(Math.random() * parent.grabDelay));                setText("     ");                rightStick.grab();                setIcon(parent.imgs[parent.RIGHTSPOONDUKE]);		Thread.sleep((int)(Math.random() * parent.grabDelay));                leftStick.grab();                setIcon(parent.imgs[parent.BOTHSPOONSDUKE]);		Thread.sleep((int)(Math.random() * parent.grabDelay));                rightStick.release();                leftStick.release();                setIcon(parent.imgs[parent.HUNGRYDUKE]);                setText("Mmmm!");                sated = true;		Thread.sleep((int)(Math.random() * parent.grabDelay * 4));                sated = false;            }        } catch (java.lang.InterruptedException e) {        }        leftStick.releaseIfMine();        rightStick.releaseIfMine();        setIcon(parent.imgs[parent.HUNGRYDUKE]);	setText(parent.names[position]);        sated = false;        philThread = new Thread(this);    }}

⌨️ 快捷键说明

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