哲学家就餐问题是1965年由Dijkstra提出的一种线程同步的问题。

问题描述:一圆桌前坐着5位哲学家,两个人中间有一只筷子,桌子中央有面条。哲学家思考问题,当饿了的时候拿起左右两只筷子吃饭,必须拿到两只筷子才能吃饭。上述问题会产生死锁的情况,当5个哲学家都拿起自己右手边的筷子,准备拿左手边的筷子时产生死锁现象。

解决办法:

  • 拿筷子之前判断两支筷子是否有人使用,都无人使用时才能拿起筷子。
  • 不能独占一支筷子,造成死锁。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class Philosopher implements Runnable { private static class Chopstick {
private boolean[] chopsticks = {true, true, true, true, true}; public synchronized void take_two(int index) {
while (!attempt(index)) {
System.out.println(String.format("--哲学家%d--尝试拿筷子--失败...", index));
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(String.format("--哲学家%d--尝试拿筷子--成功...", index));
chopsticks[getLeft(index)] = false;
chopsticks[getRight(index)] = false;
} private boolean attempt(int index) {
System.out.println(String.format("--哲学家%d--尝试拿筷子...", index));
return chopsticks[getLeft(index)] && chopsticks[getRight(index)];
} public synchronized void put_two(int index) {
System.out.println(String.format("--哲学家%d--放下筷子...", index));
chopsticks[getLeft(index)] = true;
chopsticks[getRight(index)] = true;
notifyAll();
} int getLeft(int index) {
return (index - 1 + chopsticks.length) % chopsticks.length;
} int getRight(int index) {
return index;
}
} private int index;
private Chopstick chopsticks; public Philosopher(int index, Chopstick chopsticks) {
this.index = index;
this.chopsticks = chopsticks;
} private void think() {
System.out.println(String.format("--哲学家%d--正在思考...", index));
try {
Thread.sleep(random(20 * 1000, 30 * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
} private void eat() {
System.out.println(String.format("--哲学家%d--正在吃饭...", index));
try {
Thread.sleep(random(20 * 1000, 30 * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Override
public void run() {
while (true) {
think();
chopsticks.take_two(index);
eat();
chopsticks.put_two(index);
}
} private static int random(int min, int max) {
return min + (int) (Math.random() * (max - min + 1));
} public static void main(String[] args) {
Chopstick chopsticks = new Chopstick();
Philosopher p1 = new Philosopher(0, chopsticks);
Philosopher p2 = new Philosopher(1, chopsticks);
Philosopher p3 = new Philosopher(2, chopsticks);
Philosopher p4 = new Philosopher(3, chopsticks);
Philosopher p5 = new Philosopher(4, chopsticks);
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(p1);
executorService.execute(p2);
executorService.execute(p3);
executorService.execute(p4);
executorService.execute(p5);
}
}

进程同步——哲学家进餐问题Java实现的更多相关文章

  1. java笔记--超级类Object多线程的应用+哲学家进餐算法内部类与多线程结合

    关于Object类中的线程方法: Object类是所有Java类的 父类,在该类中定义了三个与线程操作有关的方法,使得所有的Java类在创建之后就支持多线程 这三个方法是:notify(),notif ...

  2. Java哲学家进餐问题|多线程

    Java实验三 多线程 哲学家进餐问题: 5个哲学家共用一张圆桌,分别坐在周围的5张椅子上, 在圆桌上有5个碗和5只筷子(注意是5只筷子,不是5双), 碗和筷子交替排列.他们的生活方式是交替地进行思考 ...

  3. 哲学家就餐问题-Java语言实现死锁避免

    哲学家就餐问题-Java语言实现死锁避免 我死锁预防是至少破坏死锁产生的四个必要条件之一,带来的问题就是系统资源利用率低且不符合开发习惯,而死锁避免不是事先釆取某种限制措施破坏死锁的必要条件,只是注意 ...

  4. 利用Linux下的pthread_mutex_t类型来实现哲学家进餐问题

    首先说一下什么是哲学家进餐问题,这是操作系统课程中一个经典的同步问题, 问题如下:如上图,有6个哲学家和6根筷子(那个蓝色部分表示哲学家,那个紫色长条部分表示筷子),他们分别被编了0~5的号!如果某个 ...

  5. 第4章 同步控制 Synchronization ---哲学家进餐问题(The Dining Philosophers)

    哲学家进餐问题是这样子的:好几位哲学家围绕着餐桌坐,每一位哲学家要么思考,要么等待,要么就吃饭.为了吃饭,哲学家必须拿起两支筷子(分放于左右两端).不幸的是,筷子的数量和哲学家相等,所以每支筷子必须由 ...

  6. linux c语言 哲学家进餐---信号量PV方法一

    1.实验原理   由Dijkstra提出并解决的哲学家进餐问题(The Dinning Philosophers Problem)是典型的同步问题.该问题是描述有五个哲学家共用一张圆桌,分别坐在周围的 ...

  7. Java 哲学家进餐

    某次操作系统实验存档.V 这个哲学家除了吃就知道睡.( ╯□╰ ) 哲学家.java: package operating.entity.philosophyeating; import operat ...

  8. 课程设计——利用信号量实现哲学家进餐问题(JAVA)

    package cn.Douzi.PhiEat; /** * 表示筷子的类 */ public class Chopstick{ /** * 表示筷子是否可用 */ private volatile ...

  9. Java哲学家进餐

    某次操作系统实验存档. 这个哲学家除了吃就是睡.. 哲学家.java: package operating.entity.philosophyeating; import operating.meth ...

随机推荐

  1. 【贪心/Trie】【CF1083B】 The Fair Nut and Strings

    Description 有 \(k\) 个长度为 \(n\) 的只含 \(a\) 或 \(b\) 字符串,并不知道它们具体是多少,只知道它们的字典序不小于字符串 \(A\),同时不大于字符串 \(B\ ...

  2. Eureka的一些注意事项

    1.心跳设置:只能在application.yml中 2. 注册到Eureka上面的服务名称 与swagger2使用的时候,需要配置此项,否则显示服务名称为unknown 3.高可用的Eureka 4 ...

  3. egg.js路由的优雅改造

    引言 在使用express,koa, 或者是egg.js进行node server开发的过程中,我们的路由基本上都是定义在controller层的,框架对于 node 原生路由都会进行一层封装,一版都 ...

  4. 【Asp.net入门4-02】使用Visual Studio调试器

  5. vhost配置

    server { listen ; server_name local.*****.com; index index.html index.htm index.php; root /home/wwwr ...

  6. [spark]-Spark2.x集群搭建与参数详解

    在前面的Spark发展历程和基本概念中介绍了Spark的一些基本概念,熟悉了这些基本概念对于集群的搭建是很有必要的.我们可以了解到每个参数配置的作用是什么.这里将详细介绍Spark集群搭建以及xml参 ...

  7. [Linux]-Linux常用命令之文件解压

    不压缩方式压缩的文件需要不同的命令来解压缩,下面是Linux的各种文件解压命令. 对于.tar结尾的文件: tar -xf 对于.gz结尾的文件 : gzip -d all.gz gunzip all ...

  8. ORM choice字段 如何在页面上显示值

    核心:obj.get_字段名_display 1.定义module 数据结构: class msg(models.Model): choice = ( (1, '技术部'), (2, '行政'), ( ...

  9. Matlab修改背景颜色

    步骤: 1, 在matlab命令行中运行prefdir, 获取matlab.prf文件所在路径 2, 打开matlab.prf所在路径, 找到matlab.prf文件, 作备份 3, 将以下内容添加到 ...

  10. Codeforces刷题计划

    Codeforces刷题计划 已完成:-- / -- [Codeforces370E]370E - Summer Reading:构造:(给定某些数,在空白处填数,要求不下降,并且相邻差值<=1 ...