课程设计——利用信号量实现哲学家进餐问题(JAVA)
package cn.Douzi.PhiEat; /**
* 表示筷子的类
*/
public class Chopstick{ /**
* 表示筷子是否可用
*/
private volatile boolean available = true;
private int id; public Chopstick(){ } public Chopstick(int id){
this.id = id;
} public boolean isAvailable(){
return available;
} public void setAvailable(boolean available){
this.available = available;
} public int getId(){
return id;
} public void setId(int id){
this.id = id;
} @Override
public String toString(){
return "筷子" + id;
} }
package cn.Douzi.PhiEat;
public class ChopstickArray{
private Chopstick[] chopsticks;
public ChopstickArray(){
}
public ChopstickArray(int size) {
chopsticks = new Chopstick[size];
for(int i = 0; i < chopsticks.length; ++i){
chopsticks[i] = new Chopstick(i);
}
}
public Chopstick getId(int id){
return chopsticks[id];
}
//得到 编号id的筷子
public Chopstick getLast(int id){
if(id == 0){
return chopsticks[chopsticks.length - 1];
}else{
return chopsticks[id - 1];
}
}
}
package cn.Douzi.PhiEat; import java.awt.*;
import java.awt.event.*; import javax.swing.*; public class DiningPhilosophersFrame extends JFrame{ private final JPanel panel1 = new JPanel();
private final JPanel panel2 = new JPanel();
private final JPanel panel3 = new JPanel(); 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("暂停"); public DiningPhilosophersFrame(){ panel2.setLayout(new GridLayout(2, 2, 3, 3));
panel3.setLayout(new BorderLayout()); 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 = new JScrollPane(thinkingTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); JScrollPane js2 = new JScrollPane(eatingTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane js3 = new JScrollPane(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(new FlowLayout(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(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
ChopstickArray chopstickArray = new ChopstickArray(5);
for(int i = 0; i < 5; i++){
new Thread(new Philosopher(i, chopstickArray,
thinkingTextArea, eatingTextArea, waitingTextArea))
.start();
}
}
}); stop.addActionListener(new ActionListener() { @Override
public void actionPerformed(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 void main(String[] args){ new DiningPhilosophersFrame(); } }
package cn.Douzi.PhiEat;
import java.util.Random; import javax.swing.JTextArea; public class Philosopher implements Runnable{ private int id;
private boolean state;
ChopstickArray chopstickArray;
JTextArea thinkingTextArea;
JTextArea eatingTextArea;
JTextArea waitingTextArea; public Philosopher() { } public Philosopher(int id, ChopstickArray chopstickArray,
JTextArea thinkingTextArea, JTextArea eatingtextArea,
JTextArea waitingTextArea){
this.id = id;
this.chopstickArray = chopstickArray;
this.thinkingTextArea = thinkingTextArea;
this.eatingTextArea = eatingtextArea;
this.waitingTextArea = waitingTextArea;
} public synchronized void thinking() {
if(state) {
// 如果在思考,说明这个哲学家两面的筷子没用
chopstickArray.getId(id).setAvailable(true);
chopstickArray.getLast(id).setAvailable(true);
String text = thinkingTextArea.getText();
thinkingTextArea.setText(text + this + "在思考\n");
thinkingTextArea.setCaretPosition(thinkingTextArea.getDocument().getLength());
thinkingTextArea.paintImmediately(thinkingTextArea.getBounds());
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
state = false;
} public synchronized void eating(){
if(!state) {
//没有在思考
if(chopstickArray.getId(id).isAvailable()){ // 如果哲学家右手边的筷子可用
if(chopstickArray.getLast(id).isAvailable()){ // 如果左手边的筷子也可用
// 然后将这个能吃饭的哲学家两侧的筷子都设置为不可用
chopstickArray.getId(id).setAvailable(false);
chopstickArray.getLast(id).setAvailable(false);
String text = eatingTextArea.getText();
eatingTextArea.setText(text + this + "在吃饭\n");
eatingTextArea.setCaretPosition(eatingTextArea.getDocument().getLength());
eatingTextArea.paintImmediately(eatingTextArea.getBounds());
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
} else{
// 右手边的筷子可用,但是左手边的不可用
String str = waitingTextArea.getText();
waitingTextArea.setText(str + this + "在等待"
+ chopstickArray.getLast(id) + "\n");
try{
wait(new Random().nextInt(100));
}catch(Exception e){
e.printStackTrace();
}
}
}else {
// 如果哲学家右手边的筷子不可用则等待
String str = waitingTextArea.getText();
waitingTextArea.setText(str + this + "在等待"
+ chopstickArray.getId(id) + "\n");
waitingTextArea.setCaretPosition(waitingTextArea.getDocument().getLength());
waitingTextArea.paintImmediately(waitingTextArea.getBounds());
try{
wait(new Random().nextInt(100));
}catch(Exception e) {
e.printStackTrace();
}
}
}
state = true;
} @Override
public void run(){
for(int i = 0; i < 10; ++i){
thinking();
eating();
}
} @Override
public String toString(){
return " 哲学家 " + id;
} }


课程设计——利用信号量实现哲学家进餐问题(JAVA)的更多相关文章
- 课程设计——利用信号量实现生产者-消费者问题(java)
package cn.Douzi.ProductConsume; import java.util.LinkedList; import java.util.Queue; import java.ut ...
- 课程设计——利用信号量实现读-写者问题(JAVA)
package cn.Douzi.ReadWriter; import java.util.Scanner; public class ReadWrite { static public int co ...
- 进程同步——哲学家进餐问题Java实现
哲学家就餐问题是1965年由Dijkstra提出的一种线程同步的问题. 问题描述:一圆桌前坐着5位哲学家,两个人中间有一只筷子,桌子中央有面条.哲学家思考问题,当饿了的时候拿起左右两只筷子吃饭,必须拿 ...
- Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)
本文主要内容: 信号量的实现 利用信号量解决哲学家用餐问题 利用信号量解决生产者消费者问题 一.信号量的实现 1.1 信号量结构 typedef struct { int value; struct ...
- JAVA课程设计+五子棋(团队博客)
JAVA课程设计 利用所学习的JAVA知识设计一个五子棋小游戏 1.团队名称.团队成员介绍(菜鸟三人组) 杨泽斌[组长]:201521123049 网络1512 叶文柠[组员]:20152112305 ...
- Java课程设计---索引
一.基础配置 ============================================================== 1.Java课程设计---Eclipse基本环境配置 2.J ...
- 利用Linux下的pthread_mutex_t类型来实现哲学家进餐问题
首先说一下什么是哲学家进餐问题,这是操作系统课程中一个经典的同步问题, 问题如下:如上图,有6个哲学家和6根筷子(那个蓝色部分表示哲学家,那个紫色长条部分表示筷子),他们分别被编了0~5的号!如果某个 ...
- linux c语言 哲学家进餐---信号量PV方法一
1.实验原理 由Dijkstra提出并解决的哲学家进餐问题(The Dinning Philosophers Problem)是典型的同步问题.该问题是描述有五个哲学家共用一张圆桌,分别坐在周围的 ...
- Bryce1010的操作系统课程设计
https://download.csdn.net/download/fire_to_cheat_/10221003 上面是课程设计的代码,下载需要一些积分. 1.作业调度 2.磁盘调度 常见的磁盘调 ...
随机推荐
- Right-BICEP 测试四则运算程序
测试方法: Right-BICEP 测试计划: 1.边界测试是否正确 2.负数表示是否实现 3.是否有乘除法 4.是否可以选择题目数量 5.是否有输出方式 6.是否有括号 7.是否有重复查询 ...
- Alpha冲刺——第十天
Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- HDU 5159 Card
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5159 题解: 考虑没一个数的贡献,一个数一次都不出现的次数是(x-1)^b,而总的排列次数是x^b, ...
- lintcode-423-有效的括号序列
423-有效的括号序列 给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and ']', 判定是否是有效的括号序列. 样例 括号必须依照 "( ...
- J2EE体系
J2EE的概念 目前,Java 2平台有3个版本,它们是适用于小型设备和智能卡的Java 2平台Micro版(Java 2 Platform Micro Edition,J2ME).适用于桌面系统的J ...
- linux 转移mysql文件操作流程
1.现将mysql停服 2.将文件拷贝到指定目录cp ./sales_trade_2.ibd /db/data/mysql/data_warehouse/sales_trade_2.ibd 3.检查新 ...
- JAVA字节流(读写文件)
InputStream此抽象类是表示字节输入流的所有类的超类.需要定义 InputStream 的子类的应用程序必须始终提供返回下一个输入字节的方法. int available()返回此输入流方法的 ...
- TCP建立连接和断开连接过程
假设Client端发起中断连接请求,也就是发送FIN报文.Server端接到FIN报文后,意思是说"我Client端没有数据要发给你了",但是如果你还有数据没有发送完成,则不必急着 ...
- bzoj1061-[Noi2008]志愿者招募-单纯形 & 费用流
有\(n\)天,\(m\)类志愿者,一个第\(i\)类志愿者可以从第\(s_i\)天工作到第\(t_i\)天,第\(i\)天工作的志愿者不少于\(b_i\)个.每一类志愿者都有单价\(c_i\),问满 ...
- robot framework Selenium2关键字介绍
*** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arguments] ${locator} Che ...