哲学家就餐问题的代码实现

本文介绍了使用Java实现哲学家就餐问题的三种解决方案:使用AND型信号量、限制并发人数和规定不同哲学家的拿筷顺序,以防止死锁的发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.AND型信号量
(1)Chopsticks.java**
package philosopy;
import java util.*;
class Chopsticks {
public static List chops = new ArrayList();
static {
chops.add(false); //为了方便计算,第一个不会参与计算
chops.add(false);
chops.add(false);
chops.add(false);
chops.add(false);
}

public synchronized void getChop() {
String currentName = Thread.currentThread().getName();
int index = Integer.parseInt(currentName);
while (chops.get(index) || chops.get((index + 1)%5)) {//同时获得两双筷子才能进行
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
chops.set(index, true);
chops.set((index + 1)%5 ,true);
}

public synchronized void freeChop() {
String currentName = Thread.currentThread().getName();
int index = Integer.parseInt(currentName);
chops.set(index, false);//释放左筷子
chops.set((index + 1)%5 ,false);//释放右筷子
notifyAll();//通知所有人我已吃完
}
}

(2)PhilosopherThread.java类
class PhilosopherThread extends Thread {
private String name; //线程名称,给哲学家编序号用
private Chopsticks chopsticks;

public PhilosopherThread (String name, Chopsticks chopsticks) {
super(name);
// this.name = name;
this.chopsticks = chopsticks;
}

@Override
public void run() {//执行
while (true) {
chopsticks.getChop();
System.out.println(Chopsticks.chops);
this.eat();
chopsticks.freeChop();
}
}

public void eat() { //获得资源吃
try {
Thread.sleep(1000);//停留1000s
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

(3)Test.java类
public class Test {
public static void main(String[] args) {
Chopsticks chopsticks = new Chopsticks();
PhilosopherThread philosopherThread1 = new PhilosopherThread(“0”, chopsticks);
PhilosopherThread philosopherThread2 = new PhilosopherThread(“1”, chopsticks);
PhilosopherThread philosopherThread3 = new PhilosopherThread(“2”, chopsticks);
PhilosopherThread philosopherThread4 = new PhilosopherThread(“3”, chopsticks);
PhilosopherThread philosopherThread5 = new PhilosopherThread(“4”, chopsticks);

philosopherThread1.start();
philosopherThread2.start();
philosopherThread3.start();
philosopherThread4.start();
philosopherThread5.start();
}
}

得到的结果:
在这里插入图片描述

2.只允许4个人同时进餐吃饭
Test.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
//哲学家吃饭问题
public class Test {
//创建大小为5的信号量数组,模拟5根筷子
static Semaphore[] arry=new Semaphore[5];
//定义一个值为4的信号量,代表最多只能有四个哲学家拿起左边的筷子
static Semaphore leftCount=new Semaphore(4,true);
public static void main(String[] args) {
//创建一个5个线程的线程池
ExecutorService es=Executors.newFixedThreadPool(5);
//初始化信号量
for(int i=0;i<5;i++){
arry[i]=new Semaphore(1,true);
}
//创建5个哲学家 但这样有可能会产生死锁问题
for(int i=0;i<5;i++){
es.execute(new ActionRunnable(i));
}
}
//第i+1号哲学家的活动过程
static class ActionRunnable implements Runnable{
private int i=0;
ActionRunnable(int i){
this.i=i;
}
@Override
public void run() {
while(!Thread.interrupted()){
try {
//看拿起左边筷子的线程数是否已满,可以,则能拿起左边筷子的线程数减一,不能则等待
leftCount.acquire();
arry[i].acquire();
//请求右边的筷子
arry[(i+1)%5].acquire();
//吃饭
System.out.println(“我是哲学家”+(i+1)+“号我在吃饭”);
//释放左手的筷子
arry[i].release();
//能拿起左边筷子的线程数量加一
leftCount.release();
//释放右手的筷子
arry[(i+1)%5].release();
//哲学家开始思考
System.out.println(“我是哲学家”+(i+1)+“号我吃饱了我要开始思考了”);
//通知cpu 将调度权让给其他哲学家线程
Thread.yield();
//思考1秒
//把休眠关闭,造成死锁的概率就会增加
//Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

结果:
在这里插入图片描述

3.规定奇数号哲学家先拿他左边的筷子,偶数号哲学家先拿他右边的筷子
Test.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
//哲学家进餐问题
public class Test {
//创建大小为5的信号量数组,模拟5根筷子
static Semaphore[] arry=new Semaphore[5];
public static void main(String[] args) {
//创建一个5个线程的线程池
ExecutorService es=Executors.newFixedThreadPool(5);
//初始化信号量
for(int i=0;i<5;i++){
arry[i]=new Semaphore(1,true);
}
//创建5个哲学家 但这样有可能会产生死锁问题
for(int i=0;i<5;i++){
es.execute(new ActionRunnable(i));
}
}
//第i+1号哲学家的活动过程
static class ActionRunnable implements Runnable{
private int i=0;
ActionRunnable(int i){
this.i=i;
}
@Override
public void run() {
while(!Thread.interrupted()){
try {
if((i+1)%2!=0){
//奇数号哲学家
//请求左边的筷子
arry[i].acquire();
//请求右边的筷子
arry[(i+1)%5].acquire();
}else{
//偶数号哲学家
//请求右边的筷子
arry[(i+1)%5].acquire();
//再请求左边的筷子
arry[i].acquire();
}
//吃饭
System.out.println(“我是哲学家”+(i+1)+“号我在吃饭”);
if((i+1)%2!=0){
//奇数号哲学家
//释放左手的筷子
arry[i].release();
//释放右手的筷子
arry[(i+1)%5].release();
}else{
//偶数号的哲学家
arry[(i+1)%5].release();
arry[i].release();
}
//哲学家开始思考
System.out.println(“我是哲学家”+(i+1)+“号我吃饱了我要开始思考了”);
//通知cpu 将调度权让给其他哲学家线程
Thread.yield();
//思考1秒
//把休眠关闭,造成死锁的概率就会增加
//Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}运行结果:显然这种方法会造成某一个哲学家一直占据着资源的问题

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值