多线程---和尚吃馒头问题

在项目中,经常用到一种设计模式----单例模式,下面举一个小案例,说明线程安全的单例模式在多线程中的应用,以供学习参考:

    和尚吃馒头:

100个馒头,30个和尚,每个和尚最少吃一个馒头,最多不超过4个馒头,保证上述条件的情况下,
尽快将馒头吃了!

要求是严格单例模式实现篮子类(存放馒头的容器)。

  1. <span style="font-size:18px;">package java.thread;  
  2.   
  3. /** 
  4.  *  
  5.  */  
  6. public class MantouDemo {  
  7.     public static void main(String[] args) {  
  8.         for(int i = 0 ; i < Box.uneatedMonks ; i ++){  
  9.             new Monk("tom" + i).start();  
  10.         }  
  11.     }  
  12. }  
  13.   
  14. //篮子  
  15. class Box{  
  16.   
  17.     private static Box instance = null ;  
  18.   
  19.     private static Object lock = new Object();  
  20.   
  21.     //馒头总数  
  22.     private int COUNT = 100 ;  
  23.   
  24.     //没吃馒头的和尚数量  
  25.     public static int uneatedMonks = 30 ;  
  26.   
  27.     public static Box getInstance(){  
  28.         if(instance != null){  
  29.             return instance ;  
  30.         }  
  31.         synchronized (lock){  
  32.             if(instance == null){  
  33.                 instance = new Box();  
  34.             }  
  35.             return instance ;  
  36.         }  
  37.     }  
  38.   
  39.     private Box(){  
  40.     }  
  41.   
  42.     //获取馒头  
  43.     public int getMantou(Monk monk){  
  44.         //1.是否还有可吃馒头  
  45.         if(COUNT == 0){  
  46.             return 0 ;  
  47.         }  
  48.         //2.和尚是否吃饱了  
  49.         if(monk.getCount() == Monk.MAX){  
  50.             return 0 ;  
  51.         }  
  52.         //3.还有多余的馒头  
  53.         if(COUNT > uneatedMonks){  
  54.             int tmp = COUNT ;  
  55.             COUNT -- ;  
  56.             //和尚是否是第一次吃馒头  
  57.             if(monk.getCount() == 0){  
  58.                 uneatedMonks -- ;  
  59.             }  
  60.             return tmp ;  
  61.         }  
  62.         //没有多余的馒头  
  63.         else{  
  64.             if(monk.getCount() == 0){  
  65.                 int tmp = COUNT;  
  66.                 COUNT--;  
  67.                 uneatedMonks--;  
  68.                 return tmp ;  
  69.             }  
  70.         }  
  71.         return 0 ;  
  72.     }  
  73. }  
  74.   
  75. //和尚  
  76. class Monk extends Thread{  
  77.     public static int MAX = 4 ;  
  78.     public static int MIN = 1 ;  
  79.     //和尚吃了馒头的数量  
  80.     private int count = 0 ;  
  81.   
  82.     public int getCount() {  
  83.         return count;  
  84.     }  
  85.   
  86.     public void setCount(int count) {  
  87.         this.count = count;  
  88.     }  
  89.   
  90.     private String monkName ;  
  91.   
  92.     public Monk(String monkName) {  
  93.         this.monkName = monkName ;  
  94.     }  
  95.   
  96.     public void run() {  
  97.         Box box = Box.getInstance();  
  98.         while(true){  
  99.             int mantouNo = box.getMantou(this) ;  
  100.             if(mantouNo != 0){  
  101.                 count ++ ;  
  102.             }  
  103.             else{  
  104.                 break ;  
  105.             }  
  106.             yield();  
  107.         }  
  108.         System.out.println(monkName + " : " + count );  
  109.     }  
  110. }  
  111. </span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值