问题描述
由Dijkstra提出并解决的哲学家就餐问题是典型的同步问题。该问题描述的是五个哲学家共用一张圆桌,分别坐在周围的五张椅子上,在圆桌上有五个碗和五只筷子,他们的生活方式是交替的进行思考和进餐。平时,一个哲学家进行思考,饥饿时便试图取用其左右最靠近他的筷子,只有在他拿到两只筷子时才能进餐。进餐完毕,放下筷子继续思考。
图:
解答思路
面向对象编程
-
要定义哲学家对象,Philosopher,哲学家对象里面要有编号,要有左右的筷子 -
定义筷子对象 ChopStick
正常定义的情况下肯定是会出现死锁的场景。因为每个人都依赖他下面一个筷子才能吃饭 解决方法就是,让最后一个人先拿右边筷子,再拿左边筷子,这样就会有一个人会先拿到右手,又回拿到左手的情况。就会先吃完饭,释放锁
解题:有一个人先右手,就不会产生死锁
0号哲学家 先拿了 0 号筷子 4号哲学家 拿了 4 号筷子 3号哲学家 拿了 3号筷子 2号哲学家 拿了 2号筷子 1号哲学家 先去拿 0号筷子,发现0号筷子已经被拿了,就等到。这个时候 2号哲学家就能同时拿到 2号筷子和1号筷子。就吃完饭释放锁了。
同理 3号吃完 4号吃完 0号吃完 1号吃完
场景死锁锁的情况 编码
package com.leeue.demo;
/**
* 哲学家吃饭问题,死锁案例
*
* @Author liyue
* @date 2023/7/1 10:44
**/
/**
* 哲学家实体类
*/
class Philosopher extends Thread {
int index;
ChopStick left;
ChopStick right;
public Philosopher(int index, ChopStick left, ChopStick right) {
this.index = index;
this.left = left;
this.right = right;
}
@Override
public void run() {
synchronized (this.left) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this.right) {
System.out.println("哲学家:" + index + "吃上饭了");
}
}
}
}
/**
* 筷子实体类
*/
class ChopStick {
}
public class PhilosopherDeadLock {
public static void main(String[] args) {
ChopStick chopStick_0 = new ChopStick();
ChopStick chopStick_1 = new ChopStick();
ChopStick chopStick_2 = new ChopStick();
ChopStick chopStick_3 = new ChopStick();
ChopStick chopStick_4 = new ChopStick();
Philosopher philosopher_0 = new Philosopher(0, chopStick_0, chopStick_4);
Philosopher philosopher_1 = new Philosopher(1, chopStick_1, chopStick_0);
Philosopher philosopher_2 = new Philosopher(2, chopStick_2, chopStick_1);
Philosopher philosopher_3 = new Philosopher(3, chopStick_3, chopStick_2);
Philosopher philosopher_4 = new Philosopher(4, chopStick_4, chopStick_3);
philosopher_0.start();
philosopher_1.start();
philosopher_2.start();
philosopher_3.start();
philosopher_4.start();
}
}
死锁解决代码
package com.leeue.demo;
/**
* 哲学家吃饭问题,死锁案例
*
* @Author liyue
* @date 2023/7/1 10:44
**/
/**
* 哲学家实体类
*/
class Philosopher extends Thread {
int index;
ChopStick left;
ChopStick right;
public Philosopher(int index, ChopStick left, ChopStick right) {
this.index = index;
this.left = left;
this.right = right;
}
@Override
public void run() {
if (index == 0) {
synchronized (this.right) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this.left) {
System.out.println("哲学家:" + index + "吃上饭了");
}
}
} else {
synchronized (this.left) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this.right) {
System.out.println("哲学家:" + index + "吃上饭了");
}
}
}
}
}
/**
* 筷子实体类
*/
class ChopStick {
}
public class PhilosopherDeadLockOpen {
public static void main(String[] args) {
ChopStick chopStick_0 = new ChopStick();
ChopStick chopStick_1 = new ChopStick();
ChopStick chopStick_2 = new ChopStick();
ChopStick chopStick_3 = new ChopStick();
ChopStick chopStick_4 = new ChopStick();
Philosopher philosopher_0 = new Philosopher(0, chopStick_0, chopStick_4);
Philosopher philosopher_1 = new Philosopher(1, chopStick_1, chopStick_0);
Philosopher philosopher_2 = new Philosopher(2, chopStick_2, chopStick_1);
Philosopher philosopher_3 = new Philosopher(3, chopStick_3, chopStick_2);
Philosopher philosopher_4 = new Philosopher(4, chopStick_4, chopStick_3);
philosopher_0.start();
philosopher_1.start();
philosopher_2.start();
philosopher_3.start();
philosopher_4.start();
}
}
思考 1万个哲学家该如何提交效率进行吃饭。
可以多定义几个哲学家先拿右手再拿左手的逻辑。如:%2 == 0 的可以走先拿右手再拿左手的逻辑
本文由 mdnice 多平台发布