哲學家進餐問題,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.      

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章