哲学家就餐问题的思考(java实现)

前言

这是我第一眼看到该问题时想到的解决方式之一,不知道可不可行,如果大家有什么看法可以探讨探讨。

问题描述

有五个哲学家,他们的生活方式是交替地进行思考和进餐,n哲学家们共用一张圆桌,分别坐在周围的五张椅子上,在圆桌上有五个碗和五支筷子,n平时哲学家进行思考,饥饿时便试图取其左、右最靠近他的筷子,只有在他拿到两支筷子时才能进餐,n进餐完毕,放下筷子又继续思考。

约束条件
(1)只有拿到两只筷子时,哲学家才能吃饭。
(2)如果筷子已被别人拿走,则必须等别人吃完之后才能拿到筷子。
(3)任一哲学家在自己未拿到两只筷子吃饭前,不会放下手中拿到的筷子。

产生的问题

5个哲学家同时获取筷子,那么就会产生死锁。

思考

如果死锁是由于5个哲学家同时拿左手(或右手)边的筷子导致死锁的话,那为什么不改变其中一个哲学家的取筷子顺序呢,毕竟题目约束条件没说必须每个哲学家都得先拿左手(或右手)。
所以我觉得可以改变其中一个哲学家的取筷子顺序来解开死锁问题。
例如:改变第一个哲学家的取筷子顺序,让他先拿右手的筷子,其他四个哲学家都是先拿左手的筷子。这样总会有最后一个筷子空余着防止死锁产生。

java实现方法
  • 五个哲学家相当于五个线程
  • 五双筷子由一个仓库管理
五双筷子管理实现

五双筷子从编号1开始(没从0开始别喷我。。。)。

package DinningPhilosophers;

import java.util.LinkedList;

/**
 * Created by maxi on 2017/11/8.
 */
public class DinningTool {
    private LinkedList<Integer> linkedList = new LinkedList<>();

    public DinningTool() {
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);
    }

    //拿走 remove
    public void pickUp(Integer e) {
        synchronized (linkedList) {
            try {
                while (!linkedList.contains(e))
                    linkedList.wait();
                linkedList.remove(e);
                linkedList.notifyAll();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    //放下 add
    public void dropDown(Integer e) {
        synchronized (linkedList) {
            try {
                linkedList.add(e);
                linkedList.notifyAll();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}
五个哲学家实现

五个线程,其中一个线程先取右边的筷子,其他四个线程都先取左边的筷子。
例如:

哲学家编号筷子编号(按先后顺序)
11(右筷子),5(左筷子)
21(左),2(右)
32(左),3(右)
43(左),4(右)
54(左),5(右)
public static void main(String[] args) {
        DinningTool dinningTool = new DinningTool();

        //哲学家1
        Thread one = new Thread(() -> {
            int num = 0;
            while (true) {
                try {
                    dinningTool.pickUp(1);
//                    System.out.println("one start to pickup 1 right!");
                    dinningTool.pickUp(5);
//                    System.out.println("one start to pickup 5 left!");
//                    System.out.println("one start to eat!");
                    Thread.sleep(300);
                    dinningTool.dropDown(1);
//                    System.out.println("one start to dropdown 1 right!");
                    dinningTool.dropDown(5);
//                    System.out.println("one start to dropdown 5 left!");
//                    System.out.println("one start to think!");
                    Thread.sleep(500);
                    num++;
                    System.out.println("one recycle ====================================" + num);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //哲学家2
        Thread two = new Thread(() -> {
            int num = 0;
            while (true) {
                try {
                    dinningTool.pickUp(1);
//                    System.out.println("two start to pickup 1 left!");
                    dinningTool.pickUp(2);
//                    System.out.println("two start to pickup 2 right!");
//                    System.out.println("two start to eat!");
                    Thread.sleep(300);
                    dinningTool.dropDown(1);
//                    System.out.println("two start to dropdown 1 left!");
                    dinningTool.dropDown(2);
//                    System.out.println("two start to dropdown 2 right!");
//                    System.out.println("two start to think!");
                    Thread.sleep(500);
                    num++;
                    System.out.println("two recycle ====================================" + num);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //哲学家3
        Thread three = new Thread(() -> {
            int num = 0;
            while (true) {
                try {
                    dinningTool.pickUp(2);
//                    System.out.println("three start to pickup 2 left!");
                    dinningTool.pickUp(3);
//                    System.out.println("three start to pickup 3 right!");
//                    System.out.println("three start to eat!");
                    Thread.sleep(300);
                    dinningTool.dropDown(2);
//                    System.out.println("three start to dropdown 2 left!");
                    dinningTool.dropDown(3);
//                    System.out.println("three start to dropdown 3 right!");
//                    System.out.println("three start to think!");
                    Thread.sleep(500);
                    num++;
                    System.out.println("three recycle ====================================" + num);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //哲学家4
        Thread four = new Thread(() -> {
            int num = 0;
            while (true) {
                try {
                    dinningTool.pickUp(3);
//                    System.out.println("four start to pickup 3 left!");
                    dinningTool.pickUp(4);
//                    System.out.println("four start to pickup 4 right!");
//                    System.out.println("four start to eat!");
                    Thread.sleep(300);
                    dinningTool.dropDown(3);
//                    System.out.println("four start to dropdown 3 left!");
                    dinningTool.dropDown(4);
//                    System.out.println("four start to dropdown 4 right!");
//                    System.out.println("four start to think!");
                    Thread.sleep(500);
                    num++;
                    System.out.println("four recycle ====================================" + num);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //哲学家5
        Thread five = new Thread(() -> {
            int num = 0;
            while (true) {
                try {
                    dinningTool.pickUp(4);
//                    System.out.println("five start to pickup 4 left!");
                    dinningTool.pickUp(5);
//                    System.out.println("five start to pickup 5 right!");
//                    System.out.println("five start to eat!");
                    Thread.sleep(300);
                    dinningTool.dropDown(4);
//                    System.out.println("five start to dropdown 4 left!");
                    dinningTool.dropDown(5);
//                    System.out.println("five start to dropdown 5 right!");
//                    System.out.println("five start to think!");
                    Thread.sleep(500);
                    num++;
                    System.out.println("five recycle ====================================" + num);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        one.start();

        two.start();

        three.start();

        four.start();

        five.start();
    }
结束

有啥问题一定要跟我说啊!:)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值