Chinaunix首页 | 论坛 | 博客
  • 博客访问: 279757
  • 博文数量: 88
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 840
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-20 21:13
文章分类

全部博文(88)

文章存档

2022年(1)

2017年(1)

2016年(2)

2015年(1)

2014年(83)

分类: Java

2014-06-05 21:54:34

代码:

点击(此处)折叠或打开

  1. package j2se;
  2. class kuaizi{
  3.     String name;
  4.     boolean Enable = true;
  5.     
  6.     public kuaizi(String name){
  7.         this.name = name;
  8.     }
  9.     
  10.     public synchronized void pickup(){
  11.         this.Enable =false;
  12.     }
  13.     
  14.     public synchronized void putdown(){
  15.         this.Enable =true;
  16.         this.notifyAll();
  17.     }
  18.     
  19. }
  20. class Philosopher extends Thread{
  21.     String name;
  22.     kuaizi left;
  23.     kuaizi right;
  24.     public Philosopher(String name, kuaizi l, kuaizi r){
  25.         this.name = name;
  26.         left = l;
  27.         right = r;
  28.     }
  29.     
  30.     public void run(){
  31.         if(left.Enable){
  32.             left.pickup();
  33.         }
  34.         else{
  35.             while(!left.Enable){
  36.                 try{
  37.                     System.out.println(name + " 在等待 "+left.name);
  38.                     Thread.sleep(500);
  39.                 }catch (InterruptedException e){
  40.                     e.printStackTrace();
  41.                 }
  42.             }
  43.         }

  44.         System.out.println(name + " 眼明手快,以迅雷不及掩耳之势一把抓起 "+left.name);

  45.         try{
  46.             Thread.sleep(500);
  47.         }catch (InterruptedException e){
  48.             e.printStackTrace();
  49.         }

  50.         if(right.Enable){
  51.             right.pickup();
  52.         }else{
  53.             while (!left.Enable){
  54.                 try{
  55.                     System.out.println(name + "在等待 "+right.name);
  56.                     Thread.sleep(500);
  57.                 }catch (InterruptedException e){
  58.                     e.printStackTrace();
  59.                 }
  60.             }

  61.         }

  62.         System.out.println(name + " 眼明手快,以迅雷不及掩耳之势一把抓起 "+right.name);
  63.         System.out.println(name + " 左右开弓,狼吞虎咽起来");

  64.         try{
  65.             Thread.sleep(2000);
  66.         }catch (InterruptedException e){
  67.             e.printStackTrace();
  68.         }
  69.         System.out.println(name + " 酒足饭饱,打了个饱嗝,心满意足的放下了 "+left.name+" 和 " +right.name);
  70.         left.putdown();
  71.         right.putdown();
  72.     }
  73. }

  74. public class Eat1{
  75.     public static void main(String args[]){
  76.         kuaizi k1 = new kuaizi("筷子1号");
  77.         kuaizi k2 = new kuaizi("筷子2号");
  78.         kuaizi k3 = new kuaizi("筷子3号");
  79.         kuaizi k4 = new kuaizi("筷子4号");
  80.         kuaizi k5 = new kuaizi("筷子5号");
  81.         Philosopher p1 = new Philosopher("老大", k1, k2);
  82.         Philosopher p2 = new Philosopher("老二", k2, k3);
  83.         Philosopher p3 = new Philosopher("老三", k3, k4);
  84.         Philosopher p4 = new Philosopher("老四", k4, k5);
  85.         Philosopher p5 = new Philosopher("老幺", k5, k1);
  86.         p1.start();
  87.         p2.start();
  88.         p3.start();
  89.         p4.start();
  90.         p5.start();
  91.     }
  92. }

阅读(479) | 评论(0) | 转发(0) |
0

上一篇:进程状态

下一篇:OSI中的各层中的协议

给主人留下些什么吧!~~