哲学家吃饭问题java版_课程设计——利用信号量实现哲学家进餐问题(JAVA)

该博客介绍了一个使用Java实现的哲学家进餐问题的GUI模拟程序。通过创建JFrame窗口,设置布局和组件,包括哲学家状态的文本区域,以及开始和暂停按钮,展示了如何利用信号量解决并发问题。当点击开始按钮时,五个哲学家线程开始运行,每个哲学家随机选择思考、吃饭或等待状态,模拟了经典的哲学家进餐场景。
摘要由CSDN通过智能技术生成

packagecn.Douzi.PhiEat;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class DiningPhilosophersFrame extendsJFrame{private final JPanel panel1 = newJPanel();private final JPanel panel2 = newJPanel();private final JPanel panel3 = newJPanel();private final JTextArea thinkingTextArea = new JTextArea(6, 10);private final JTextArea eatingTextArea = new JTextArea(5, 10);private final JTextArea waitingTextArea = new JTextArea(5, 10);

JLabel label1= new JLabel("哲学家问题");

JLabel label2= new JLabel("思考");

JLabel label3= new JLabel("吃饭");

JLabel label4= new JLabel("等待");

JLabel space= new JLabel(" ");

JButton button= new JButton("开始运行");

JButton stop= new JButton("暂停");publicDiningPhilosophersFrame(){

panel2.setLayout(new GridLayout(2, 2, 3, 3));

panel3.setLayout(newBorderLayout());

label2.setFont(new Font("隶书", 0, 30));

label2.setHorizontalAlignment(0);

label3.setFont(new Font("隶书", 0, 30));

label3.setHorizontalAlignment(0);

label4.setFont(new Font("隶书", 0, 30));

label4.setHorizontalAlignment(0);

panel2.add(label2);

panel2.add(label3);

panel2.add(label4);

thinkingTextArea.setEditable(false);

eatingTextArea.setEditable(false);

waitingTextArea.setEditable(false);

JScrollPane js1= newJScrollPane(thinkingTextArea,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

JScrollPane js2= newJScrollPane(eatingTextArea,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

JScrollPane js3= newJScrollPane(waitingTextArea,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

panel2.add(js1);

panel2.add(js2);

panel2.add(js3);//panel2.add(label2);//panel2.add(label3);//panel2.add(label4);

panel2.setBackground(Color.LIGHT_GRAY);

panel1.setBackground(Color.LIGHT_GRAY);

panel1.setLayout(newFlowLayout(FlowLayout.CENTER));

label1.setFont(new Font("隶书", 0, 50)); //标题字体

panel1.add(label1);

panel1.add(panel2);

button.setBackground(Color.ORANGE);

stop.setBackground(Color.ORANGE);

panel1.add(button);

panel1.add(space);

panel1.add(stop);

setContentPane(panel1);

button.addActionListener(newActionListener(){

@Overridepublic voidactionPerformed(ActionEvent e){

ChopstickArray chopstickArray= new ChopstickArray(5);for(int i = 0; i < 5; i++){new Thread(newPhilosopher(i, chopstickArray,

thinkingTextArea, eatingTextArea, waitingTextArea))

.start();

}

}

});

stop.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {//TODO Auto-generated method stub

try{

Thread.sleep(3000);

}catch(InterruptedException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

}

});

setTitle("可爱的豆豆");

setSize(450, 400);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}public static voidmain(String[] args){newDiningPhilosophersFrame();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值