//加synchronize进行同步
//释放资源又很快获得自身的资源,这样不妥,吃完的话休息100ms //每个人先申请编号小的筷子 public class Philosopher implements Runnable {
int[] fork=new int[5];
Thread thread1=new Thread(this,"1");
Thread thread2=new Thread(this,"2");
Thread thread3=new Thread(this,"3");
Thread thread4=new Thread(this,"4");
Thread thread5=new Thread(this,"5");
public void run() {
try {
while (true) {
if (Thread.currentThread().getName().equals("1")) {
while (fork[0]==1) {
synchronized (this) {
wait();
}
}
fork[0]=1;
while (fork[1]==1) {
synchronized (this) {
wait();
}
}
fork[1]=1;
System.out.println("1 eats for 3 seconds");
Thread.sleep(3000);
fork[0]=0;
fork[1]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("2")) {
while (fork[1]==1) {
synchronized(this) {
wait();
}
}
fork[1]=1;
while (fork[2]==1) {
synchronized(this) {
wait();
}
}
fork[2]=1;
System.out.println("2 eats for 3 seconds");
Thread.sleep(3000);
fork[1]=0;
fork[2]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("3")) {
while (fork[2]==1) {
synchronized(this) {
wait();
}
}
fork[2]=1;
while (fork[3]==1) {
synchronized(this) {
wait();
}
}
fork[3]=1;
System.out.println("3 eats for 3 seconds");
Thread.sleep(3000);
fork[2]=0;
fork[3]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("4")) {
while (fork[3]==1) {
synchronized(this) {
wait();
}
}
fork[3]=1;
while (fork[4]==1) {
synchronized(this) {
wait();
}
}
fork[4]=1;
System.out.println("4 eats for 3 seconds");
Thread.sleep(3000);
fork[3]=0;
fork[4]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("5")) {
while (fork[0]==1) {
synchronized(this) {
wait();
}
}
fork[0]=1;
while (fork[4]==1) {
synchronized(this) {
wait();
}
}
fork[4]=1;
System.out.println("5 eats for 3 seconds");
Thread.sleep(3000);
fork[0]=0;
fork[4]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
}
} catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
Philosopher phi=new Philosopher();
for (int i=0;i<5;i++)
phi.fork[i]=0;
phi.thread1.start();
phi.thread2.start();
phi.thread3.start();
phi.thread4.start();
phi.thread5.start();
}
} //当某个线程试图等待一个自己并不拥有的对象(O)的监控器或者通知其他线程等待该对象(O)的监控器时,抛出该异常。
//让刚吃完的一个人阻塞,5根筷子供4个人选,则必有一个人获得在其左右的两双筷子

public class Philosopher1 implements Runnable {
int[] ifeat=new int[5];
int[] fork=new int[5];
int noteat;
Thread thread1=new Thread(this,"1");
Thread thread2=new Thread(this,"2");
Thread thread3=new Thread(this,"3");
Thread thread4=new Thread(this,"4");
Thread thread5=new Thread(this,"5");
public void run() {
try {
while (true) {
if (Thread.currentThread().getName().equals("1")) {
while (ifeat[0]==1) {
synchronized (this) {
wait();
}
}
while (fork[0]==1) {
synchronized (this) {
wait();
}
}
fork[0]=1;
while (fork[1]==1) {
synchronized (this) {
wait();
}
}
fork[1]=1;
System.out.println("1 eats for 3 seconds");
Thread.sleep(3000);
fork[0]=0;
fork[1]=0;
ifeat[noteat]=0;
noteat=0;
ifeat[0]=1;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("2")) {
while (ifeat[1]==1) {
synchronized (this) {
wait();
}
}
while (fork[1]==1) {
synchronized (this) {
wait();
}
}
fork[1]=1;
while (fork[2]==1) {
synchronized (this) {
wait();
}
}
fork[2]=1;
System.out.println("2 eats for 3 seconds");
Thread.sleep(3000);
fork[1]=0;
fork[2]=0;
ifeat[noteat]=0;
noteat=1;
ifeat[1]=1;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("3")) {
while (ifeat[2]==1) {
synchronized (this) {
wait();
}
}
while (fork[2]==1) {
synchronized (this) {
wait();
}
}
fork[2]=1;
while (fork[3]==1) {
synchronized (this) {
wait();
}
}
fork[3]=1;
System.out.println("3 eats for 3 seconds");
Thread.sleep(3000);
fork[2]=0;
fork[3]=0;
ifeat[noteat]=0;
noteat=2;
ifeat[2]=1;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("4")) {
while (ifeat[3]==1) {
synchronized (this) {
wait();
}
}
while (fork[3]==1) {
synchronized (this) {
wait();
}
}
fork[3]=1;
while (fork[4]==1) {
synchronized (this) {
wait();
}
}
fork[4]=1;
System.out.println("4 eats for 3 seconds");
Thread.sleep(3000);
fork[3]=0;
fork[4]=0;
ifeat[noteat]=0;
noteat=3;
ifeat[3]=1;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("5")) {
while (ifeat[4]==1) {
synchronized (this) {
wait();
}
}
while (fork[4]==1) {
synchronized (this) {
wait();
}
}
fork[4]=1;
while (fork[0]==1) {
synchronized (this) {
wait();
}
}
fork[0]=1;
System.out.println("5 eats for 3 seconds");
Thread.sleep(3000);
fork[4]=0;
fork[0]=0;
ifeat[noteat]=0;
noteat=4;
ifeat[4]=1;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
}
} catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
Philosopher1 phi=new Philosopher1();
for (int i=0;i<5;i++)
phi.fork[i]=0; phi.ifeat[0]=1;
for (int i=0;i<5;i++)
phi.ifeat[i]=0;
phi.noteat=0; phi.thread1.start();
phi.thread2.start();
phi.thread3.start();
phi.thread4.start();
phi.thread5.start();
}
}
//只有两双筷子都有,才获取,且同时获取两双筷子

public class Philosopher2 implements Runnable {
int[] fork=new int[5];
Thread thread1=new Thread(this,"1");
Thread thread2=new Thread(this,"2");
Thread thread3=new Thread(this,"3");
Thread thread4=new Thread(this,"4");
Thread thread5=new Thread(this,"5");
public void run() {
try {
while (true) {
if (Thread.currentThread().getName().equals("1")) {
while (fork[0]==1 || fork[1]==1) {
synchronized (this) {
wait();
}
}
fork[0]=1;
fork[1]=1;
System.out.println("1 eats for 3 seconds");
Thread.sleep(3000);
fork[0]=0;
fork[1]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("2")) {
while (fork[1]==1 || fork[2]==1) {
synchronized (this) {
wait();
}
}
fork[1]=1;
fork[2]=1;
System.out.println("2 eats for 3 seconds");
Thread.sleep(3000);
fork[1]=0;
fork[2]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("3")) {
while (fork[2]==1 || fork[3]==1) {
synchronized (this) {
wait();
}
}
fork[2]=1;
fork[3]=1;
System.out.println("3 eats for 3 seconds");
Thread.sleep(3000);
fork[2]=0;
fork[3]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("4")) {
while (fork[3]==1 || fork[4]==1) {
synchronized (this) {
wait();
}
}
fork[3]=1;
fork[4]=1;
System.out.println("4 eats for 3 seconds");
Thread.sleep(3000);
fork[3]=0;
fork[4]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
else if (Thread.currentThread().getName().equals("5")) {
while (fork[0]==1 || fork[4]==1) {
synchronized (this) {
wait();
}
}
fork[0]=1;
fork[4]=1;
System.out.println("5 eats for 3 seconds");
Thread.sleep(3000);
fork[0]=0;
fork[4]=0;
synchronized(this) {
notifyAll();
}
Thread.sleep(100);
}
}
} catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
Philosopher2 phi=new Philosopher2();
for (int i=0;i<5;i++)
phi.fork[i]=0;
phi.thread1.start();
phi.thread2.start();
phi.thread3.start();
phi.thread4.start();
phi.thread5.start();
}
}

哲学家问题(java)的三个解法的更多相关文章

  1. LeetCode算法题-Move Zeroes(Java实现-三种解法)

    这是悦乐书的第201次更新,第211篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第67题(顺位题号是283).给定一个数组nums,写一个函数将所有0移动到它的末尾,同 ...

  2. LeetCode算法题-First Bad Version(Java实现-三种解法)

    这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...

  3. LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

    这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...

  4. LeetCode算法题-Number Complement(Java实现-五种解法)

    这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...

  5. LeetCode算法题-Third Maximum Number(Java实现-四种解法)

    这是悦乐书的第222次更新,第235篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第89题(顺位题号是414).给定非空的整数数组,返回此数组中的第三个最大数字.如果不存 ...

  6. LeetCode算法题-Find the Difference(Java实现-五种解法)

    这是悦乐书的第214次更新,第227篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第82题(顺位题号是389).给定两个字符串s和t,它们只包含小写字母.字符串t由随机混 ...

  7. LeetCode算法题-Valid Perfect Square(Java实现-四种解法)

    这是悦乐书的第209次更新,第221篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第77题(顺位题号是367).给定正整数num,写一个函数,如果num是一个完美的正方形 ...

  8. LeetCode算法题-Intersection of Two Arrays(Java实现-四种解法)

    这是悦乐书的第207次更新,第219篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第75题(顺位题号是349).给定两个数组,编写一个函数来计算它们的交集.例如: 输入: ...

  9. LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)

    这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...

随机推荐

  1. Codeforces1151E,F | 553Div2 | 瞎讲报告

    传送链接 E. Number of Components 当时思博了..一直在想对于\([1,r]\)的联通块和\([1,l-1]\)的联通块推到\([l,r]\)的联通块...我真的是傻了..这题明 ...

  2. linux压缩相关

    tar命令 tar是打包,即把好多东西放在一个大文件里面,之后再压缩:当然也可以解包 tar的几个参数说明: -c 创建一个新的包 -x 将包里的文件还原出来 -t 显示包内文件的列表 -f 指定要处 ...

  3. 随手记录-linux-vim使用

  4. 【贪心算法】POJ-1017

    一.题目 Description A factory produces products packed in square packets of the same height h and of th ...

  5. 韩剧TV APP案例分析

    产品 选择产品:韩剧TV 版本:Android版 选择理由:节假日坐车回家时使用较多次数的APP,刚好国庆坐车回家时正在使用,所以选择了这款APP. 第一部分:调研.评测 第一次上手体验 刚打开APP ...

  6. ThinkPhp输入参数过滤

    I('id',0); // 获取id参数 自动判断get或者post,不存在时返回默认值0 I('post.name','','htmlspecialchars'); //获取$_POST['name ...

  7. 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (四) 树莓派单子节点查询

    考虑到项目的实际需要,树莓派作为主机,应该只在需要的时候查询特定节点发送的数据,因此接收到数据后需要根据头部判断是否是自己需要的数据,如果不是继续接收数据,超过一定时间未查询到特定节点的数据,则退出程 ...

  8. gitlab账号注册及分组

    .开启注册邮箱验证 admin area -->setting-->Sign-up restrictions-->勾选Send confirmation email on sign- ...

  9. scrapy 直接在编辑器运行

    # *_*coding:utf-8 *_* from scrapy.cmdline import execute execute("scrapy crawl spbeen --nolog&q ...

  10. notepad++ 安装go插件

    1. 想学习go语言 使用notepad++ 但是发现无法安装 gonpp的插件 花了很长时间. 发现问题为: 前几天将notepad++ 升级到了 7.6 的版本 然后使用 plugin manag ...