您的位置:首页 > 编程语言 > Java开发

消费者模式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();

}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: