消费者模式java多线程之哲学家进餐问题(5人5筷)
来源:互联网 发布:淘宝商城宠物狗 编辑:程序博客网 时间:2024/06/11 19:35
消费者模式之哲学家进餐问题
/**
* Description: 哲学家吃饭
* Copyright (c) , 2016, Jansonxu
* This program is protected by copyright laws.
* Program Name:PhilosopheEat.java
* Date: 2016年1月26日
*
* @author 李阳
* @version : 1.0
*/
package PhysicsEatFish;
public class PhilosopheEat {
class Chopstic{
private int id;//编号
private boolean state;//true 被占用 false 未被占用
/**
*
*/
public Chopstic() {
// TODO Auto-generated constructor stub
}
public Chopstic(int id, boolean state) {
super();
this.id = id;
this.state = state;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
@Override
public String toString() {
return “Chopstic [id=” + id + “, state=” + state + “]”;
}
}//哲学家线程类class Philosophe extends Thread{ private String pname; private Chopstic left; private Chopstic right; /* (non-Javadoc) * @see java.lang.Thread#toString() */ @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } /** * */ public Philosophe() { // TODO Auto-generated constructor stub } public Philosophe(String pname, Chopstic left, Chopstic right) { super(); this.pname = pname; this.left = left; this.right = right; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Chopstic getLeft() { return left; } public void setLeft(Chopstic left) { this.left = left; } public Chopstic getRight() { return right; } public void setRight(Chopstic right) { this.right = right; } @Override public void run() { while(true){ synchronized ("") { //判断左筷子是否被占用 if(left.isState()){ try { "".wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 走到下边说明未被占用设置状态为 true left.setState(true); //a 被占用 就等待 //b 否则就拿起 //2 判断右筷子是否被占用 //a 占用 放下左筷子 //b 占用 放下左筷子 if(right.isState()){ left.setState(false); //放下左筷子之后 也要等待 try { "".wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //走到这 说明 右筷子也未被使用 设置状态为 true right.setState(true); System.out.println("哲学家【"+pname+"】拿起了 左筷子 【"+left+"】 右筷子【"+right+"】 开始进餐"); //进餐后 放下左右的筷子 通知别的哲学家 吃东西 left.setState(false); right.setState(false); //通知别的哲学家 进餐 "".notifyAll(); } //通知之后 睡会 // 写在外边 让其他人 获得筷子 try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } }}// 最完美的时刻 应该是 两个一起吃 同一时刻 两个吃 public static void main(String[] args) { //步骤 //构建外部类对象 PhilosopheEat outer=new PhilosopheEat(); //构建5个筷子 Chopstic one=outer.new Chopstic(1,false); Chopstic two=outer.new Chopstic(2,false); Chopstic three=outer.new Chopstic(3,false); Chopstic four=outer.new Chopstic(4,false); Chopstic five=outer.new Chopstic(5,false); //构建五个哲学家 Philosophe kz=outer.new Philosophe("孔子",one,five); Philosophe mz=outer.new Philosophe("孟子",two,one); Philosophe lz=outer.new Philosophe("老子",three,two); Philosophe hfz=outer.new Philosophe("韩非子",four,three); Philosophe sz=outer.new Philosophe("孙子",five,four); //开启线程 kz.start(); mz.start(); lz.start(); hfz.start(); sz.start();}
}
- 消费者模式java多线程之哲学家进餐问题(5人5筷)
- Java 多线程-哲学家进餐问题
- java 多线程实现 哲学家进餐问题
- 哲学家进餐问题与生产者与消费者问题(java实现)
- C-多线程-哲学家进餐问题
- Java实现哲学家进餐问题
- 多线程 生产者&消费者 哲学家进餐 & random & synchronized & Thread & Sleep
- 经典IPC问题之哲学家进餐问题
- 经典同步问题之哲学家进餐
- 尝试解决哲学家进餐问题(Java实现)
- 经典IPC问题(哲学家进餐)
- 哲学家进餐问题(dining philosopher problem)
- 哲学家进餐问题
- 哲学家进餐的问题
- 哲学家进餐问题
- 哲学家进餐问题
- 关于哲学家进餐问题
- 哲学家进餐问题
- Oracle VM VirtualBox上安装ubuntu
- springMvc配置详细讲解
- [IOS 开发] 无限后台运行
- 正则表达式匹配域名、网址、url
- 线性代数
- 消费者模式java多线程之哲学家进餐问题(5人5筷)
- 解决java java.io.FileNotFoundException: http:\localhost:8080 (文件名、目录名或卷)问题
- 抽象工厂模式
- Spring4.x with scheduling task
- Monochromatic Triangles[POI1997,bzoj2916]
- PHP编码规范/PSR-2
- 快速简单的定制一个时间轴布局(LinearLayout)
- 王学岗Viewpager(一)
- DataTable如何去除重复的行