java面向对象思路解决哲学家就餐问题

一、问题分析
大家都先拿左手筷子,那大家都吃不了饭(一把筷子相当于一把锁,都等待右手锁释放)

继续分析:只要有一人同时拿到左右手筷子就可以解决这个问题,他吃了放下边上人再吃。

方案:引入一个左撇子。四个人拿左边的,一个人拿右边的。有一个人必然双手持有筷子。(仔细缕缕)

在这里插入图片描述

如图4哲学家先右手

(1)4没抢到(就会阻塞,一直尝试右手拿筷子),则其他都是左手拿到了,0号右手边的筷子一直没人拿,这个时候0号拿到右手先吃,其他人等着,0吃完放下筷子,1吃,以此类推都吃了。
(2)4号先手抢到了,3干瞪眼,其他人都左手拿到了,这个时候0、4中间有一双闲置的,随便谁抢到都可以,吃完解锁大家都吃。

这个时候有个问题,五个人依次吃效率太低了,所以引入一半左撇子,就可以提高效率

二、代码如下

public class Chopsticks {
}
public class Philosopher implements Runnable{
    public  Chopsticks left;
    public  Chopsticks right;

    public Integer num;


    public Philosopher(Chopsticks left, Chopsticks right,Integer num) {
        this.left = left;
        this.right = right;
        this.num=num;
    }

    @Override
    public void run() {
        int i = num%2;
        if(i==1){
            synchronized (left){
                sleep(3000);
                synchronized(right){
                    sleep(3000);
                    System.out.println("吃完了"+Thread.currentThread().getName());
                }
            }
        }else {
            synchronized (right){
                sleep(3000);
                synchronized(left){
                    sleep(3000);
                    System.out.println("吃完了"+Thread.currentThread().getName());
                }
            }
        }

    }

    public static void sleep(long millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public static void main(String[] args) {
        Chopsticks chopsticks0 = new Chopsticks();
        Chopsticks chopsticks1 = new Chopsticks();
        Chopsticks chopsticks2 = new Chopsticks();
        Chopsticks chopsticks3 = new Chopsticks();
        Chopsticks chopsticks4 = new Chopsticks();

        Philosopher philosopher0 = new Philosopher(chopsticks0, chopsticks1,0);
        Philosopher philosopher1 = new Philosopher(chopsticks1, chopsticks2,1);
        Philosopher philosopher2 = new Philosopher(chopsticks2, chopsticks3,2);
        Philosopher philosopher3 = new Philosopher(chopsticks3, chopsticks4,3);
        Philosopher philosopher4 = new Philosopher(chopsticks4, chopsticks0,4);


        Thread thread0 = new Thread(philosopher0);
        thread0.setName("哲学0");
        Thread thread1 = new Thread(philosopher1);
        thread1.setName("哲学1");
        Thread thread2 = new Thread(philosopher2);
        thread2.setName("哲学2");
        Thread thread3 = new Thread(philosopher3);
        thread3.setName("哲学3");
        Thread thread4 = new Thread(philosopher4);
        thread4.setName("哲学4");

        thread0.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread1.start();

    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哲学家就餐问题可以使用Java语言通过线程控制来实现。 具体实现如下: 1. 定义一个Chopstick类表示筷子,其中包含一个标识唯一性的id属性。 ``` class Chopstick { private int id; public Chopstick(int id) { this.id = id; } public int getId() { return id; } } ``` 2. 定义一个Philosopher类表示哲学家,其中包含一个标识唯一性的id属性和两只筷子属性,表示他需要用到的两只筷子。 ``` class Philosopher implements Runnable { private int id; private Chopstick leftChopstick; private Chopstick rightChopstick; public Philosopher(int id, Chopstick leftChopstick, Chopstick rightChopstick) { this.id = id; this.leftChopstick = leftChopstick; this.rightChopstick = rightChopstick; } @Override public void run() { try { while (true) { // 哲学家思考 think(); // 哲学家拿起左边的筷子 synchronized (leftChopstick) { System.out.println("Philosopher " + id + " picked up " + leftChopstick.getId() + " chopstick."); // 哲学家拿起右边的筷子 synchronized (rightChopstick) { System.out.println("Philosopher " + id + " picked up " + rightChopstick.getId() + " chopstick."); // 哲学家就餐 eat(); } } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } private void think() throws InterruptedException { System.out.println("Philosopher " + id + " is thinking."); Thread.sleep(new Random().nextInt(1000)); } private void eat() throws InterruptedException { System.out.println("Philosopher " + id + " is eating."); Thread.sleep(new Random().nextInt(1000)); System.out.println("Philosopher " + id + " put down " + rightChopstick.getId() + " chopstick."); System.out.println("Philosopher " + id + " put down " + leftChopstick.getId() + " chopstick."); } } ``` 3. 在main方法中创建五个哲学家和五只筷子,并启动每个哲学家的线程。 ``` public class DiningPhilosophers { 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, chopstick1, chopstick2); Philosopher philosopher2 = new Philosopher(2, chopstick2, chopstick3); Philosopher philosopher3 = new Philosopher(3, chopstick3, chopstick4); Philosopher philosopher4 = new Philosopher(4, chopstick4, chopstick5); Philosopher philosopher5 = new Philosopher(5, chopstick5, chopstick1); new Thread(philosopher1).start(); new Thread(philosopher2).start(); new Thread(philosopher3).start(); new Thread(philosopher4).start(); new Thread(philosopher5).start(); } } ``` 在程序运行时,哲学家们会不断地思考、拿起筷子、就餐和放下筷子,直到程序被手动终止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值