哲学家问题(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).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...
随机推荐
- Java-URLEncoder.encode 什么时候才是必须的
当你希望把一段 URL 当成另一个 URL 的参数时,比如:当用户点击交易的按钮时你发现未登录就跳转到 login 页面同时带上一个参数记录在登录之前用户是希望访问的那个交易页面,这样在登录完成之后再 ...
- Thunder——爱阅app(测评人:方铭)
B.Thunder——爱阅app(测评人:方铭) 一.基于NABCD评论作品,及改进建议 每个小组评论其他小组Alpha发布的作品: 1.根据(不限于)NABCD评论作品的选题: 2.评论作品对选题的 ...
- spring冲刺第三天
昨天完成了环境配置和初步的地图设想. 今天从网上找了有关这方面的例子,运行试验了一番.编写的地图画面在程序上运行了一下,有些错误,还需要很多方面的改进. 这些例子有很多地方都不太懂,但还是看完了.我认 ...
- 【CS231N】1、图像分类
一.知识点 1. 计算机识别物体面临的困难 视角变化(Viewpoint variation):同一个物体,摄像机可以从多个角度来展现. 大小变化(Scale variation):物体可视的大小通常 ...
- 【图论】POJ-3723 最大生成树
一.题目 Description Windy has a country, and he wants to build an army to protect his country. He has p ...
- springboot maven
更多信息请从官网获取https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE 1.parent基于自己项目而非spring-boot-starter- ...
- JAVA异常架构图及常见面试题
红色为检查异常,就是eclipse要提示你是try catch 还是throws. 非检查异常,就是/0,nullpointexception,数据越界访问indexOfOutBounds 异常 错误 ...
- Python开发【第五篇】迭代器、生成器、递归函数、二分法
阅读目录 一.迭代器 1. 迭代的概念 #迭代器即迭代的工具(自定义的函数),那什么是迭代呢? #迭代:指一个重复的过程,每次重复都可以称之为一次迭代,并且每一次重复的结果是下一个迭代的初始值(例如: ...
- 利用css制作带边框的小三角
标签(空格分隔):css 在项目中会使用到的小实例,目前知道的有两种方法来实现 设置元素的宽和高,利用rotate实现,比较简单的一种 div{ width: 10px; height: 10px; ...
- 清除浮动小记,兼容Ie6,7
.clearfix { *zoom:1;} .clearfix:after{clear:both; display:block; height:0; visibility:hidden; line-h ...