📄 diningphilosophers.java
字号:
/*
* DiningPhilosophers.java
*
* Created on 2007年7月15日, 上午12:34
*/
package processSyn;
import java.awt.*;
import java.awt.geom.*;
/**
*
* @author jzf
*/
public class DiningPhilosophers extends javax.swing.JPanel {
/** Creates new form DiningPhilosophers */
public DiningPhilosophers() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
startButton = new javax.swing.JButton();
stopButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
list = new java.awt.List();
startButton.setText("\u5f00\u59cb");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startButtonActionPerformed(evt);
}
});
stopButton.setText("\u505c\u6b62");
stopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopButtonActionPerformed(evt);
}
});
resetButton.setText("\u91cd\u7f6e");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(startButton)
.addGap(67, 67, 67)
.addComponent(stopButton)
.addGap(61, 61, 61)
.addComponent(resetButton)
.addGap(127, 127, 127))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(234, Short.MAX_VALUE)
.addComponent(list, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(resetButton)
.addComponent(stopButton)
.addComponent(startButton))
.addGap(22, 22, 22)
.addComponent(list, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
// TODO 将在此处添加您的处理代码:
stop=true;
philo1Eating=false;
philo2Eating=false;
philo3Eating=false;
philo4Eating=false;
philo5Eating=false;
if(p1!=null||p2!=null||p3!=null||p4!=null||p5!=null){
p1.stop();
p2.stop();
p3.stop();
p4.stop();
p5.stop();
}
list.removeAll();
repaint();
}//GEN-LAST:event_resetButtonActionPerformed
private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
// TODO 将在此处添加您的处理代码:
stop=true;
if(p1!=null||p2!=null||p3!=null||p4!=null||p5!=null){
p1.stop();
p2.stop();
p3.stop();
p4.stop();
p5.stop();
}
}//GEN-LAST:event_stopButtonActionPerformed
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
// TODO 将在此处添加您的处理代码:
stop=false;
k1 = new kuai("筷子1号");
k2 = new kuai("筷子2号");
k3 = new kuai("筷子3号");
k4 = new kuai("筷子4号");
k5 = new kuai("筷子5号");
p1=new People("哲学家1", k1, k2);
p2=new People("哲学家2", k2, k3);
p3=new People("哲学家3", k3, k4);
p4=new People("哲学家4", k4, k5);
p5=new People("哲学家5", k5, k1);
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
}//GEN-LAST:event_startButtonActionPerformed
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D) g;
if(philo1Eating){
Color color=g.getColor();
g.setColor(Color.YELLOW);
g2.fillOval(97,60,65,65);
g.setColor(color);
g.drawString("Eating...",107,95);
} else{
Color color=g.getColor();
g.setColor(Color.GREEN);
g2.fillOval(97,60,65,65);
g.setColor(color);
g.drawString("Thinking...",105,95);
}
if(philo2Eating){
Color color=g.getColor();
g.setColor(Color.YELLOW);
g2.fillOval(10,130,65,65);
g.setColor(color);
g.drawString("Eating...",18,165);
} else{
Color color=g.getColor();
g.setColor(Color.GREEN);
g2.fillOval(10,130,65,65);
g.setColor(color);
g.drawString("Thinking...",18,165);
}
if(philo3Eating){
Color color=g.getColor();
g.setColor(Color.YELLOW);
g2.fillOval(43,230,65,65);
g.setColor(color);
g.drawString("Eating...",48,267);
} else{
Color color=g.getColor();
g.setColor(Color.GREEN);
g2.fillOval(43,230,65,65);
g.setColor(color);
g.drawString("Thinking...",48,267);
}
if(philo4Eating){
Color color=g.getColor();
g.setColor(Color.YELLOW);
g2.fillOval(150,230,65,65);
g.setColor(color);
g.drawString("Eating...",156,267);
} else{
Color color=g.getColor();
g.setColor(Color.GREEN);
g2.fillOval(150,230,65,65);
g.setColor(color);
g.drawString("Thinking...",156,267);
}
if(philo5Eating){
Color color=g.getColor();
g.setColor(Color.YELLOW);
g2.fillOval(180,130,65,65);
g.setColor(color);
g.drawString("Eating...",189,164);
} else{
Color color=g.getColor();
g.setColor(Color.GREEN);
g2.fillOval(180,130,65,65);
g.setColor(color);
g.drawString("Thinking...",187,164);
}
g.drawString("1号",117,82);
g.drawString("2号",33,151);
g.drawString("3号",65,252);
g.drawString("4号",170,252);
g.drawString("5号",203,151);
}
class kuai {
String name; // 筷子的名字
boolean Enable = true; // 初始化筷子为可用
public kuai(String name) {
this.name = name;
}
public synchronized void pickup() {
try {
while (Enable == false) { // 如果筷子不可用,就等待。
this.wait();
}
this.Enable = false;
} catch (Exception e) {
}
}
public synchronized void putdown() {
this.Enable = true;
this.notifyAll(); // 唤醒其他等待的进程
}
}
class People extends Thread {
String name;
kuai left;
kuai right;
public People(String name, kuai l, kuai r) { // 构造函数初始化
this.name = name;
left = l;
right = r;
}
public void run() {
while (!stop) {
left.pickup();
right.pickup();
DiningPhilosophers.this.list.add(name + "抓起"+left.name);
DiningPhilosophers.this.list.add(name + "抓起"+right.name);
DiningPhilosophers.this.list.add(name + "开始吃饭");
if(name.equals("哲学家1")){
philo1Eating=true;
philo2Eating=false;
philo3Eating=false;
philo4Eating=false;
philo5Eating=false;
}
if(name.equals("哲学家2")){
philo2Eating=true;
philo1Eating=false;
philo3Eating=false;
philo4Eating=false;
philo5Eating=false;
}
if(name.equals("哲学家3")){
philo3Eating=true;
philo1Eating=false;
philo2Eating=false;
philo4Eating=false;
philo5Eating=false;
}
if(name.equals("哲学家4")){
philo4Eating=true;
}
if(name.equals("哲学家5")){
philo5Eating=true;
philo1Eating=false;
philo2Eating=false;
philo3Eating=false;
philo4Eating=false;
}
DiningPhilosophers.this.repaint();
try {
Thread.sleep((int)Math.random()*8000+5000); // 随机睡眠2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
DiningPhilosophers.this.list.add(name + "吃完,放下了"+ left.name + "和"+ right.name);
left.putdown();
right.putdown();
if(name.equals("哲学家1")){
philo1Eating=false;
}
if(name.equals("哲学家2")){
philo2Eating=false;
}
if(name.equals("哲学家3")){
philo3Eating=false;
}
if(name.equals("哲学家4")){
philo4Eating=false;
}
if(name.equals("哲学家5")){
philo5Eating=false;
}
DiningPhilosophers.this.repaint();
}
}
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private java.awt.List list;
private javax.swing.JButton resetButton;
private javax.swing.JButton startButton;
private javax.swing.JButton stopButton;
// 变量声明结束//GEN-END:variables
private boolean philo1Eating=false,philo2Eating=false,philo3Eating=false,philo4Eating=false,philo5Eating=false;
//private boolean chop1Using=false,chop2Using=false,chop3Using=false,chop4Using=false,chop5Using=false;
private boolean stop=true;
private kuai k1,k2,k3,k4,k5;
private People p1,p2,p3,p4,p5;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -