哲学家问题(java)的三个解法
//加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)的三个解法的更多相关文章
- LeetCode算法题-Move Zeroes(Java实现-三种解法)
这是悦乐书的第201次更新,第211篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第67题(顺位题号是283).给定一个数组nums,写一个函数将所有0移动到它的末尾,同 ...
- LeetCode算法题-First Bad Version(Java实现-三种解法)
这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...
- LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)
这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...
- LeetCode算法题-Number Complement(Java实现-五种解法)
这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...
- LeetCode算法题-Third Maximum Number(Java实现-四种解法)
这是悦乐书的第222次更新,第235篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第89题(顺位题号是414).给定非空的整数数组,返回此数组中的第三个最大数字.如果不存 ...
- LeetCode算法题-Find the Difference(Java实现-五种解法)
这是悦乐书的第214次更新,第227篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第82题(顺位题号是389).给定两个字符串s和t,它们只包含小写字母.字符串t由随机混 ...
- LeetCode算法题-Valid Perfect Square(Java实现-四种解法)
这是悦乐书的第209次更新,第221篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第77题(顺位题号是367).给定正整数num,写一个函数,如果num是一个完美的正方形 ...
- LeetCode算法题-Intersection of Two Arrays(Java实现-四种解法)
这是悦乐书的第207次更新,第219篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第75题(顺位题号是349).给定两个数组,编写一个函数来计算它们的交集.例如: 输入: ...
- LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)
这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...
随机推荐
- 使用spring整合Quartz实现—定时器
使用spring整合Quartz实现—定时器(Maven项目做演示) 不基于特定的基类的方法 一,开发环境以及依赖的jar包 Spring 4.2.6.RELEASE Maven 3.3.9 Jdk ...
- mysql 修改语句及耗时
1.含有某串字母的字段替换: update imagetable set imageID = replace(imageID, 'ZH0211001', 'ZH4111001') 只要imageID含 ...
- 【每日scrum】第一次冲刺day5
请教以前做过类似软件的同学,受益匪浅,启发自己
- html border画三角形
最近遇到了问题就是画推进条类似于
- Mevan(转)
Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0问题解决 ojdbc包pom.xml出错 置顶 2017年08月23日 10:55:25 阅读数:96 ...
- PAT 1001 A+B Fotmat
源码 1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calcula ...
- Leetcode题库——14.最长公共前缀
@author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...
- Week2-作业1 《构建之法》1、2、16章观后感
这几天阅读了<构建之法>中的几章,受益匪浅,刷新了很多我对软件工程的认知.这本书让我很惊喜,阅读起来不像其他书一样枯燥,有很多人物的设计,以及对话的形式,非常有趣. 第一章.概述 读完第一 ...
- H5页面的滚动条在windows浏览器下始终看到(灰色的不可用的)
一般这种情况是在某些相关的div上设置了overflow:scroll属性,在mac系统的浏览器下均没有滚动条显示而在windows下的各个浏览器上均可以看到灰色的不可用的滚动条,这种情况我们需要在b ...
- KEIL C51 printf格式化输出特殊用法
作者:dragoniye 发布:2014-02-15 12:44 分类:硬件 抢沙发 /*******************************************KEI ...