编程语言
首页 > 编程语言> > Java多线程—哲学家进餐问题

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();
    }
}

标签:index,进餐,Java,ChopStick,Philosopher,right,mutex,new,多线程
来源: https://www.cnblogs.com/alvin103/p/16098254.html