//加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();
}
}

最新文章

  1. 《WePayUI组件设计的秘密》——2016年第一届前端体验大会分享
  2. Struts1.x 中的 Validate 框架
  3. ios源码-ios游戏源码-ios源码下载
  4. Python Decorator 和函数式编程
  5. Android VideoView简单播放视频
  6. ubunt下的MinimalCD
  7. Gradle Tips#1-tasks
  8. SocketAsyncEventArgs使用解说
  9. -Android的发展webservice-号码归属地查询
  10. Android手机图片适配问题
  11. PHP 八种基本的数据类型小结
  12. jackson xml转对象 对象转xml
  13. FlowerVisor理解
  14. drf框架使用之 路飞学城(第一天)
  15. lua tasklib 之lumen 分析
  16. hdu 4022 Bombing
  17. js导出excel表格中较长数字串会变成科学计数法问题
  18. spring配置和下载
  19. HP ALM lis
  20. Android 布局学习之——LinearLayout属性baselineAligned的作用及baseline

热门文章

  1. Python发送邮件(最全)
  2. 敏捷开发与XP实践
  3. 读书笔记 之 java编程思想3
  4. Beta Scrum Day 4 — 听说
  5. mac下使用marsedit写博客
  6. 使用testng多线程来测试成交编号重复的问题
  7. js学习1
  8. htop操作方法
  9. Windows系统下Log4Net+FileBeat+ELK日志分析系统问题总结
  10. 卸载Visual Studio最佳方法难道真的是重装系统?