哲学家吃饭java,哲学家吃饭有关问题模拟(java写的)

哲学家吃饭问题模拟(java写的)

这里是两种算法,公用一个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 threads = new ArrayList();

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 threads = new ArrayList();

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先。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值