//加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. BP神经网络算法推导

    目录 前置知识 梯度下降法 激活函数 多元复合函数求偏导的相关知识 正向计算 符号定义 输入层 隐含层 输出层 误差函数 反向传播 输出层与隐含层之间的权值调整 隐含层与输入层之间权值的调整 计算步骤 ...

  2. XSS-DVWA

    1.反射型 LOW: 没有过滤,直接键入PAYLOAD 查看源码 这里没有任何过滤,使用htmlspecialchars()过滤 结果不弹窗 MEDIUM: LOW等级的方法不奏效了 观察输出可能是过 ...

  3. 树莓派3+rtl8812au开启monitor模式

    首先要有一块树莓派,要有一块rtl8812au的网卡. 这个网卡是支持monitor模式的,但是我原来装的驱动驱动在raspbian上开启monitor模式时提示,找不到设备. 然后换了一个驱动 ht ...

  4. a标签的href为空的问题

    在表格里写一个a标签链接刷新表格的时候,没注意,把a标签的href设置为""空字符串,导致每次刷新表格之后会再刷新一次整体页面,找了很久都没发现问题出在哪里,最后无意之间,鼠标在一 ...

  5. Daily Scrum8 11.12

    昨天的任务已完成. 今日任务: 徐钧鸿:个人作业 张艺:构建带有用户管理的框架,并将后端移植好的代码连结. 黄可嵩:完成搜索移植 徐方宇:研究httpclient如何运作,如何利用它实现服务器和客户端 ...

  6. 始入OO课程的殿堂,初识面向对象的奥妙——OO第一次博客总结

    当我满怀期待叩开OO的大门,却发现宝藏藏在层层阻难之后 第一次作业 1.度量分析 >关于第一次作业的metrics图分析没有出现标红的McCabe Cyclomatic Complexity或者 ...

  7. web11 Struts处理表单数据

    电影网站:www.aikan66.com 项目网站:www.aikan66.com游戏网站:www.aikan66.com图片网站:www.aikan66.com书籍网站:www.aikan66.co ...

  8. java怎样把后台值传到前台

    后台代码:if(this.Request["type"]!=null){aaa=this.Request["type"].ToString(); try{bbb ...

  9. BETA随笔6/7

    前言 我们居然又冲刺了·六 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 新方案代码比之前的更简单,但是对场景的要求相应变高了,已经实现,误差感人 代码 ...

  10. asp.net文件上传接收不到文件 Request.files["']等于null

    这个时候你应该检查下你的form表单里面是否配置了这个: enctype ="multipart/form-data"  新手容易出错哦. <form id="fo ...