Java多线程—哲学家进餐问题

2022/4/4 9:19:26

本文主要是介绍Java多线程—哲学家进餐问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

哲学家进餐问题

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

代码模拟

public class Philosopher extends Thread{
    private ChopStick left, right;
    private int index;

    public Philosopher(ChopStick left, ChopStick right, int index) {
        this.left = left;
        this.index = index;
        this.right = right;
    }

    @Override
    public void run() {
        synchronized (left) {
            try {
               Thread.sleep(3000L);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            System.out.println(index + "等待right筷子");
            synchronized (right) {
               System.out.println(index + "拿到right筷子,吃饭");
            }
        }
    }

    public static void main(String[] args) {
        ChopStick chopsticks1 = new ChopStick();
        ChopStick chopsticks2 = new ChopStick();
        ChopStick chopsticks3 = new ChopStick();
        ChopStick chopsticks4 = new ChopStick();
        ChopStick chopsticks5 = new ChopStick();

        String mutex="lock";
        Philosopher philosopherThread1 = new Philosopher(chopsticks1, chopsticks2, 1,mutex);
        Philosopher philosopherThread2 = new Philosopher(chopsticks2, chopsticks3, 2,mutex);
        Philosopher philosopherThread3 = new Philosopher(chopsticks3, chopsticks4, 3,mutex);
        Philosopher philosopherThread4 = new Philosopher(chopsticks4, chopsticks5, 4,mutex);
        Philosopher philosopherThread5 = new Philosopher(chopsticks5, chopsticks1, 5,mutex);
        philosopherThread1.start();
        philosopherThread2.start();
        philosopherThread3.start();
        philosopherThread4.start();
        philosopherThread5.start();
    }
}

解决1 一次取所有资源

public class Philosopher extends Thread{
    private ChopStick left, right;
    private int index;

    private String mutex;

    public Philosopher(ChopStick left, ChopStick right, int index,String mutex) {
        this.left = left;
        this.index = index;
        this.right = right;
        this.mutex = mutex;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (mutex) {
            System.out.println(index + "拿到两个筷子,吃饭");
        }
    }

    public static void main(String[] args) {
        ChopStick chopsticks1 = new ChopStick();
        ChopStick chopsticks2 = new ChopStick();
        ChopStick chopsticks3 = new ChopStick();
        ChopStick chopsticks4 = new ChopStick();
        ChopStick chopsticks5 = new ChopStick();

        String mutex="lock";
        Philosopher philosopherThread1 = new Philosopher(chopsticks1, chopsticks2, 1,mutex);
        Philosopher philosopherThread2 = new Philosopher(chopsticks2, chopsticks3, 2,mutex);
        Philosopher philosopherThread3 = new Philosopher(chopsticks3, chopsticks4, 3,mutex);
        Philosopher philosopherThread4 = new Philosopher(chopsticks4, chopsticks5, 4,mutex);
        Philosopher philosopherThread5 = new Philosopher(chopsticks5, chopsticks1, 5,mutex);
        philosopherThread1.start();
        philosopherThread2.start();
        philosopherThread3.start();
        philosopherThread4.start();
        philosopherThread5.start();
    }
}

解决2 奇数哲学家先取右边,偶数先取左边

public class Philosopher extends Thread{
    private ChopStick left, right;
    private int index;

    private String mutex;

    public Philosopher(ChopStick left, ChopStick right, int index,String mutex) {
        this.left = left;
        this.index = index;
        this.right = right;
        this.mutex = mutex;
    }

    @Override
    public void run() {
        if(index %2 == 1){
            synchronized (right) {
                try {
                    Thread.sleep(3000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(index + "等待right筷子");
                synchronized (left) {
                    System.out.println(index + "拿到right筷子,吃饭");
                }
            }
        } else {
            synchronized (left) {
                try {
                    Thread.sleep(3000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(index + "等待right筷子");
                synchronized (right) {
                    System.out.println(index + "拿到right筷子,吃饭");
                }
            }
        }

    }

    public static void main(String[] args) {
        ChopStick chopsticks1 = new ChopStick();
        ChopStick chopsticks2 = new ChopStick();
        ChopStick chopsticks3 = new ChopStick();
        ChopStick chopsticks4 = new ChopStick();
        ChopStick chopsticks5 = new ChopStick();

        String mutex="lock";
        Philosopher philosopherThread1 = new Philosopher(chopsticks1, chopsticks2, 1,mutex);
        Philosopher philosopherThread2 = new Philosopher(chopsticks2, chopsticks3, 2,mutex);
        Philosopher philosopherThread3 = new Philosopher(chopsticks3, chopsticks4, 3,mutex);
        Philosopher philosopherThread4 = new Philosopher(chopsticks4, chopsticks5, 4,mutex);
        Philosopher philosopherThread5 = new Philosopher(chopsticks5, chopsticks1, 5,mutex);
        philosopherThread1.start();
        philosopherThread2.start();
        philosopherThread3.start();
        philosopherThread4.start();
        philosopherThread5.start();
    }
}



这篇关于Java多线程—哲学家进餐问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程