消费者模式java多线程之哲学家进餐问题(5人5筷)
2016-01-26 13:24
465 查看
消费者模式之哲学家进餐问题
/*** 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内存分配和String类型的深度解析
- spring mvc 文件上传
- struts2 helloworld
- 一个经典例子让你彻彻底底理解java回调机制
- Spring实现AOP的4种方式
- jacob操作office后关闭不掉进程
- Hello Spring Framework——源(Resources)
- Java控制语句——switch语句
- Spring中PropertyPlaceholderConfigurer的使用
- Java基础知识点2:hashCode()方法
- Eclipse 下用WiFi调试手机免root
- Struts标签入门
- Material Design学习之 Sliders(详细分析,悬空气球显示进度值,附带Eclipse可以jar)
- Material Design学习之 Sliders(详细分析,悬空气球显示进度值,附带Eclipse可以jar)
- 如何做实时监控?—— 参考 Spring Boot 实现
- Spring 从零開始-05
- Eclipse如何添加、删除jar包。
- spring如何使用多个xml配置文件
- java中的编码问题
- mava 打包时,带源码包