哲学家进餐问题,java实现

 废话不多说,代码开始:

  
  
  
  
  1. public class Scientist extends Thread { 
  2.     /* 
  3.      * @author:Bore
  4.      * @mail:[email protected] 
  5.      * @time:2010/12/7 
  6.      * 1.哲学家进餐,5个哲学家,5只筷子 
  7.      * 2.哲学家通过getChopsticks()方法,来取筷子。 
  8.      * 3.当哲学家同时获得左边和右边两只筷子后才可以进餐。否则把已经得到的筷子也释放 
  9.      * 4.如果一直筷子也没有,进程阻塞,等待其他哲学家释放资源 
  10.      * 5.进程后,释放已经获得的资源 
  11.      */ 
  12.  
  13.     static byte[] chopsticks={1,1,1,1,1};//五只筷子 
  14.  
  15.     public Scientist(String name){ 
  16.         super(name); 
  17.     } 
  18.     public void run() { 
  19.         getChopsticks(); 
  20.     } 
  21.      
  22.     public void getChopsticks(){//开始抢筷子 
  23.         int tem=Integer.parseInt(this.getName().substring(3));//获取哲学家编号 
  24.         if(tem==0){//如果是第一位科学家,先抢左边的筷子 
  25.             if(leftChopsticks(tem)){//如何获取了左边的筷子 
  26.                 if(rightChopsticks(tem)){//如果获取了右边的筷子 
  27.                     eating();//开始吃饭 
  28.                     freeLeftChopsticks(tem);//释放左边筷子 
  29.                     freeRightChopsticks(tem);//释放右边筷子 
  30.                 }else
  31.                     freeLeftChopsticks(tem); 
  32.                     System.out.println("由于"+this.getName()+"无法获得右手边的筷子,所以他把已获得的左手的筷子也释放了!"); 
  33.                     try { 
  34.                         this.sleep(1000); 
  35.                     } catch (InterruptedException e) { 
  36.                         e.printStackTrace(); 
  37.                     } 
  38.                     getChopsticks(); 
  39.                 } 
  40.             }else
  41.                 System.out.println(this.getName()+"暂时无法获取两只筷子,准备休眠!"); 
  42.                 try { 
  43.                     this.sleep(1000); 
  44.                 } catch (InterruptedException e) { 
  45.                     e.printStackTrace(); 
  46.                 } 
  47.                 getChopsticks(); 
  48.                  
  49.             }    
  50.         }else{//其他情况先抢右边的筷子 
  51.             if(rightChopsticks(tem)){//先抢右手边的筷子。 
  52.                 if(leftChopsticks(tem)){//如果获得了右手边的。去抢左手边的筷子。 
  53.                     eating();//如果获得了两只筷子,开始吃饭 
  54.                     freeLeftChopsticks(tem);//吃完了。释放左手边的筷子 
  55.                     freeRightChopsticks(tem);//释放右手边的筷子 
  56.                 }else
  57.                     freeRightChopsticks(tem); 
  58.                     System.out.println("由于"+this.getName()+"无法获得左手边的筷子,所以他把已获得的右手的筷子也释放了!"); 
  59.                     try { 
  60.                         this.sleep(1000); 
  61.                     } catch (InterruptedException e) { 
  62.                         e.printStackTrace(); 
  63.                     } 
  64.                     getChopsticks(); 
  65.                 } 
  66.             }else
  67.                 System.out.println(this.getName()+"暂时无法获取两只筷子,准备休眠!"); 
  68.                 try { 
  69.                     this.sleep(1000); 
  70.                 } catch (InterruptedException e) { 
  71.                     e.printStackTrace(); 
  72.                 } 
  73.                 getChopsticks(); 
  74.             } 
  75.         } 
  76.          
  77.          
  78.     } 
  79.      
  80.     public boolean leftChopsticks(int tem){//获取左手边筷子 
  81.         if(chopsticks[tem]==1){ 
  82.             chopsticks[tem]=0
  83.             System.out.println(this.getName()+"左手边筷子已获得!"); 
  84.             return true
  85.         }else
  86.             System.out.println(this.getName()+"左手边筷子已被哲学家"+(tem-1)+"抢走!"); 
  87.             return false
  88.         } 
  89.     } 
  90.      
  91.     public boolean rightChopsticks(int tem){//获取右手边筷子 
  92.         int i=(tem+1)%5
  93.         if(chopsticks[i]==1){ 
  94.             chopsticks[i]=0
  95.             System.out.println(this.getName()+"右手边筷子已获得!"); 
  96.             return true
  97.         }else
  98.             System.out.println(this.getName()+"右手边筷子已被哲学家"+i+"抢走!"); 
  99.             return false
  100.         } 
  101.     } 
  102.      
  103.     public void freeLeftChopsticks(int tem){//获取左手边筷子 
  104.             chopsticks[tem]=1
  105.             System.out.println(this.getName()+"左手边筷子已释放!"); 
  106.     } 
  107.      
  108.     public void freeRightChopsticks(int tem){//获取右手边筷子 
  109.         int i=(tem+1)%5
  110.         chopsticks[i]=1;     
  111.         System.out.println(this.getName()+"右手边筷子已释放!"); 
  112.     } 
  113.      
  114.     public void eating(){//开始进餐 
  115.         System.out.println("*"+this.getName()+"两只手都有了筷子,所以开始吃饭!"); 
  116.     } 
  117.      
  118.  
  119.  
  120.     /** 
  121.      * 主函数 
  122.      */ 
  123.     public static void main(String[] args) { 
  124.         for(int i=0; i<5; i++){ 
  125.             new Scientist("哲学家"+i).start(); 
  126.         } 
  127.     } 
  128.      

 

你可能感兴趣的:(java,休闲,哲学家,哲学家进餐,进餐)