这里是两种算法,公用一个chopstick类:
Chopstick类:
public class Chopstick {
/**
* 此筷子是否可以拿起
*/
private boolean enable;
/**
* 此筷子的名称
*/
public String name;
public Chopstick(boolean enable, String name) {
super();
this.enable = enable;
this.name = name;
}
public Chopstick(String name) {
this(true,name);
}
public void setEnable(boolean enbl){
this.enable = enbl;
}
public boolean getEnable(){
return this.enable;
}
/**
* 拿起筷子
*/
public synchronized void pickup(){
try{
while(this.enable == false){
wait();
}
this.enable = false;
}catch(Exception e){
}
}
/**
* 放下手中的筷子
*/
public synchronized void pickdown(){
this.enable = true;
this.notifyAll();
}
}
Philosopher类
public class Philosopher extends Thread{
private String name;
Chopstick leftChopstick;
Chopstick rightChopstick;
public Philosopher(String name, Chopstick leftChopstick,
Chopstick rightChopstick) {
super();
this.name = name;
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}
@Override
public void run() {
super.run();
leftChopstick.pickup();
System.out.println(this.name+"拿起了左手的筷子"+leftChopstick.name);
rightChopstick.pickup();
System.out.println(this.name+"拿起了右手的筷子"+rightChopstick.name);
System.out.println(this.name+"开始吃饭了");
try {
this.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.name+"吃饱了");
rightChopstick.pickdown();
System.out.println(this.name+"放下了右手的筷子"+this.rightChopstick.name);
leftChopstick.pickdown();
System.out.println(this.name+"放下了左手的筷子"+this.leftChopstick.name);
}
}
Philosopher2
public class Philosopher2 extends Thread {
private String name;
Chopstick leftChopstick;
Chopstick rightChopstick;
public static int WITHOUTCHOPSTICK = 0;
public static int WITHONECHOPSTICK = 1;
public static int WITHTWOCHOPSTICK = 2;
public static int withChopstickStatus = 0;
public Philosopher2(String name, Chopstick leftChopstick,
Chopstick rightChopstick) {
super();
this.name = name;
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}
@Override
public void run() {
super.run();
while (true) {
if (this.withChopstickStatus == this.WITHOUTCHOPSTICK) {
leftChopstick.pickup();
System.out.println(this.name + "拿起了左手的" + leftChopstick.name);
this.withChopstickStatus = this.WITHONECHOPSTICK;
if (this.rightChopstick.getEnable() == true) {
rightChopstick.pickup();
System.out.println(this.name + "拿起了右手的"
+ rightChopstick.name);
this.withChopstickStatus = this.WITHTWOCHOPSTICK;
System.out.println(this.name + "开始吃饭了");
try {
this.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.name + "吃饱了");
rightChopstick.pickdown();
System.out.println(this.name + "放下了右手的"
+ this.rightChopstick.name);
leftChopstick.pickdown();
System.out.println(this.name + "放下了左手的"
+ this.leftChopstick.name);
this.withChopstickStatus = this.WITHOUTCHOPSTICK;
break;
} else {
this.leftChopstick.pickdown();
System.out.println(this.name + "放下了左手的"
+ this.leftChopstick.name + ",因为右手的"
+ this.rightChopstick.name + "没法拿");
this.withChopstickStatus = this.WITHOUTCHOPSTICK;
try {
this.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
dining类
public class Dining {
/**
* @param args
*/
public static void main(String[] args) {
Chopstick chopstick1 = new Chopstick("筷子 1 ");
Chopstick chopstick2 = new Chopstick("筷子 2 ");
Chopstick chopstick3 = new Chopstick("筷子 3 ");
Chopstick chopstick4 = new Chopstick("筷子 4 ");
Chopstick chopstick5 = new Chopstick("筷子 5 ");
Philosopher philosopher1 = new Philosopher("哲学家 1 ", chopstick5,chopstick1);
Philosopher philosopher2 = new Philosopher("哲学家 2 ", chopstick1,chopstick2);
Philosopher philosopher3 = new Philosopher("哲学家 3 ", chopstick2,chopstick3);
Philosopher philosopher4 = new Philosopher("哲学家 4 ", chopstick3,chopstick4);
Philosopher philosopher5 = new Philosopher("哲学家 5 ", chopstick4,chopstick5);
long startTime = System.currentTimeMillis();
ArrayList<Thread> threads = new ArrayList<Thread>();
threads.add(philosopher1);
threads.add(philosopher2);
threads.add(philosopher3);
threads.add(philosopher4);
threads.add(philosopher5);
philosopher2.start();
philosopher4.start();
philosopher1.start();
philosopher3.start();
philosopher5.start();
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(System.currentTimeMillis() - startTime);
}
}
Dining2类
public class Dining2 {
/**
* @param args
*/
public static void main(String[] args) {
Chopstick chopstick1 = new Chopstick("筷子 1 ");
Chopstick chopstick2 = new Chopstick("筷子 2 ");
Chopstick chopstick3 = new Chopstick("筷子 3 ");
Chopstick chopstick4 = new Chopstick("筷子 4 ");
Chopstick chopstick5 = new Chopstick("筷子 5 ");
Philosopher2 philosopher1 = new Philosopher2("哲学家 1 ", chopstick5,chopstick1);
Philosopher2 philosopher2 = new Philosopher2("哲学家 2 ", chopstick1,chopstick2);
Philosopher2 philosopher3 = new Philosopher2("哲学家 3 ", chopstick2,chopstick3);
Philosopher2 philosopher4 = new Philosopher2("哲学家 4 ", chopstick3,chopstick4);
Philosopher2 philosopher5 = new Philosopher2("哲学家 5 ", chopstick4,chopstick5);
long startTime = System.currentTimeMillis();
ArrayList<Thread> threads = new ArrayList<Thread>();
threads.add(philosopher1);
threads.add(philosopher2);
threads.add(philosopher3);
threads.add(philosopher4);
threads.add(philosopher5);
philosopher2.start();
philosopher4.start();
philosopher1.start();
philosopher3.start();
philosopher5.start();
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(System.currentTimeMillis() - startTime);
}
}
为了防止5位哲学家同时拿筷子,同时等或是同时放下。可以让哲学家2和4先。
分享到:
相关推荐
JAVA实现哲学家就餐问题
哲学家进餐问题,java实现
哲学家吃饭问题(vc代码) 哲学家吃饭问题(vc代码) 哲学家吃饭问题(vc代码) 哲学家吃饭问题(vc代码) 哲学家吃饭问题(vc代码)
java编写的哲学家就餐问题的解决方案,有图形界面,我是重庆大学的。如果你也是重庆大学的,那就请毫不犹豫的下载吧,肯定是你需要的代码,信号量
实现一个模拟哲学家进餐问题的系统,要求用户选用哪一种算法进行哲学家进餐演示。 Main 类:初始化主界面类,它结合 javaFx 提供的可图化界面设计来设计主界面 MainController 类:处理主界面鼠标选择事件的类,用来...
哲学家吃饭问题 c++编程 操作系统 实例
模拟操作系统中哲学家就餐问题,是java编写的图形界面
java解哲学家就餐问题 哲学家进餐问题是一个多线程运用的经典例子,涉及到线程同步/互斥,临界区访问问题以及一个避免死锁的解决方法。 有五个哲学家绕着圆桌坐,每个哲学家面前有一盘面,两人之间有一支筷子,...
用来描述windows操作系统下用Java语言模拟哲学家进餐的问题算法
哲学家进餐问题的c++模拟实现,避免死锁的方法是奇数号哲学家先拿右边的筷子,偶数号先那左边的筷子,有详尽的注释。
用信号量解决不死锁的哲学家问题 java实现
生产者和消费者问题以及哲学家就餐问题,JAVA实现的程序.rar产者和消费者问题以及哲学家就餐问题,JAVA实现的程序.rar
使用mfc编写的有界面的哲学家进餐问题模拟
JAVA 哲学家就餐问题
操作系统课程设计:哲学家进餐问题的模拟实现,包括源代码、可执行的工程
有五个哲学家,他们的生活方式是交替地进行思考和进餐。他们共用一张圆桌,分别坐在五张椅子上。 在圆桌上有五个碗和五把叉子,平时...哲学家进餐问题可看作是并发进程并发执行时处理共享资源的一个有代表性的问题。
用Java实现的哲学家就餐问题,多线程,是操作系统的作业,很不错
哲学家就餐问题java swing演示版,直观演示哲学家进餐情况。 基于 java 1.6, eclipse kepler书写 直接运行压缩包内:DiningPhilosophers.jar文件,或者把项目导入eclipse运行DiningPhilosophers.java程序
关于java多线程的运用中五个哲学家的问题。五个哲学家分别围坐在的圆桌旁,每个哲学家面前只有一根筷子,关于哲学家如何吃饭的问题。
用java实现的模拟哲学家就餐的程序,一共三双筷子,三个哲学家。。。