Java多线程应用——生产者消费者


Java多线程应用——生产者消费者:

  1. import java.util.Random;  
  2. /* 
  3. *   @topic:用互斥实现生产者消费者问题 
  4. */  
  5. public class Custom{  
  6.     public static void main(String[] args) {  
  7.         FruitBasket fb = new FruitBasket();  
  8.         new Thread(new Farmer("农夫1",fb)).start();  
  9.         new Thread(new Farmer("农夫2",fb)).start();  
  10.         new Thread(new Farmer("农夫3",fb)).start();  
  11.         new Thread(new Child("小孩1",fb)).start();  
  12.         new Thread(new Child("小孩2",fb)).start();  
  13.         new Thread(new Child("小孩3",fb)).start();  
  14.     }  
  15. }  
  16. class Fruit{  
  17.     private int id;  
  18.     private static int number = 0;  
  19.     private String variety;  
  20.     private String[] varietys = "苹果,桃子,梨子,香蕉,西瓜,荔枝,葡萄".split(",");  
  21.     public Fruit(){  
  22.         this.variety = varietys[new Random().nextInt(7)];  
  23.         this.id = ++number;  
  24.     }  
  25.     public int getId(){  
  26.         return this.id;  
  27.     }  
  28.     public String getVariety(){  
  29.         return this.variety;  
  30.     }  
  31. }  
  32.   
  33. class FruitBasket{  
  34.     private Fruit[] fruits = new Fruit[10];//容量为10的水果数组   
  35.     private int index = 0;//下一个将要放入水果的位置   
  36.   
  37.     public boolean isEmpty(){//判断水果篮是否为空   
  38.         return index == 0 ?true:false;  
  39.     }  
  40.   
  41.     public boolean isFull(){//是否为满   
  42.         return index == fruits.length?true:false;  
  43.     }  
  44.   
  45.     public synchronized void push(String name , Fruit fruit){//对push方法实行同步   
  46.         while(isFull()){  
  47.             try{  
  48.                 this.wait();//若为满,则不能push,只能等待。   
  49.             }catch(InterruptedException e){  
  50.                 e.printStackTrace();  
  51.             }  
  52.         }  
  53.         fruits[index++] = fruit;  
  54.         System.out.println(name + "向水果框中放入编号为" + fruit.getId() + "的"+fruit.getVariety());  
  55.         display();  
  56.         this.notify();  //通知另一线程   
  57.     }  
  58.   
  59.     public synchronized Fruit pop(String name){  
  60.         while(isEmpty()){  
  61.             try{  
  62.                 this.wait();  
  63.             }catch(Exception e){  
  64.                 e.printStackTrace();  
  65.             }  
  66.         }  
  67.         Fruit fruit = fruits[--index];  
  68.         System.out.println(name + "从水果框中拿出编号为"+fruit.getId() +"的" + fruit.getVariety());  
  69.         display();  
  70.         this.notify();  
  71.         return fruit;  
  72.     }  
  73.   
  74.     public void display(){  
  75.         for(int i = 0 ; i < index ; i++){  
  76.             System.out.printf(fruits[i].getId()+fruits[i].getVariety() + " |");  
  77.         }  
  78.         for (int i = index; i < fruits.length; i++) {  
  79.             System.out.printf( "[" + (i + 1) + "]|");  
  80.         }  
  81.         System.out.println("\n");  
  82.     }  
  83. }  
  84.   
  85. class Farmer implements Runnable{  
  86.     private String name ;  
  87.     private FruitBasket fruitBasket;  
  88.     public void run(){//不断往篮子中放   
  89.         while(true){  
  90.             fruitBasket.push(name,new Fruit());  
  91.             try{  
  92.                 Thread.sleep(new Random().nextInt(2000));  
  93.             }catch(Exception e){  
  94.                 e.printStackTrace();  
  95.             }  
  96.         }  
  97.     }  
  98.     Farmer(String name, FruitBasket fruitBasket) {  
  99.         this.name = name;  
  100.         this.fruitBasket = fruitBasket;  
  101.     }  
  102.   
  103. }  
  104.   
  105. class Child implements Runnable{  
  106.     private String name;  
  107.     private FruitBasket fruitBasket;  
  108.     public void run(){//不断往篮子中取   
  109.         while(true){  
  110.             fruitBasket.pop(this.name);  
  111.             try{  
  112.                 Thread.sleep(new Random().nextInt(5000));  
  113.             }catch(Exception e){  
  114.                 e.printStackTrace();  
  115.             }  
  116.         }  
  117.     }  
  118.     Child(String name, FruitBasket fruitBasket) {  
  119.         this.name = name;  
  120.         this.fruitBasket = fruitBasket;  
  121.     }  
  122. }  

相关内容