使用semaphore实现生产者-消费者简单模型

时间:2021-10-03 14:56:42

代码很简单,就不介绍了。需要注意的是,先有生产才有消费,所以与消费者关联的semaphore计数开始为0,而与生产者关联的semaphore计数开始为1。


实现:

[java] view plain copy
  1. import java.util.concurrent.Semaphore;  
  2.   
  3. /** 
  4.  *   
  5.  * Multithreading: consumer-producer model using semaphores 
  6.  *  
  7.  * @author ljs 
  8.  * 2011-08-19 
  9.  * 
  10.  */  
  11. public class ConsumerProducer {     
  12.     //consumer-producer model   
  13.     static class SharedData{  
  14.         int data = 0;         
  15.         Semaphore semproducer = new Semaphore(1);  
  16.         //set initial count to 0, so a consumer needs to wait until data is available  
  17.         Semaphore semconsumer = new Semaphore(0);   
  18.           
  19.         public int consume() throws InterruptedException{     
  20.             try {  
  21.                 semconsumer.acquire();  
  22.                 System.out.format("    Consumed: %d%n", data);  
  23.                 Thread.sleep(10);  
  24.                 return data;  
  25.             } catch (InterruptedException e) {    
  26.                 throw e;  
  27.             } finally{  
  28.                 semproducer.release();  
  29.             }                     
  30.         }  
  31.           
  32.         public void produce(int data) throws InterruptedException{            
  33.             try {  
  34.                 semproducer.acquire();                
  35.                 this.data = data;     
  36.                 System.out.format("Produced: %d%n", data);                
  37.                 Thread.sleep(10);  
  38.             } catch (InterruptedException e) {  
  39.                 throw e;  
  40.             } finally{  
  41.                 semconsumer.release();  
  42.             }     
  43.         }  
  44.     }  
  45.     //consumer  
  46.     static class ConsumerThread implements Runnable  {  
  47.    
  48.         private SharedData shared;  
  49.            
  50.         public ConsumerThread(SharedData shared){  
  51.             this.shared=shared;  
  52.         }  
  53.           
  54.         public void run(){            
  55.             for(int i=0;i<30;i++){  
  56.                 try {  
  57.                     int data = shared.consume();  
  58.                     //use data  
  59.                 } catch (InterruptedException e) {  
  60.                     e.printStackTrace();  
  61.                 }                 
  62.             }  
  63.         }  
  64.     }  
  65.     //producer  
  66.     static class ProducerThread implements Runnable{  
  67.         private SharedData shared;  
  68.         public ProducerThread(SharedData shared){  
  69.             this.shared=shared;  
  70.         }  
  71.         public void run(){  
  72.             for(int i=0;i<30;i++){  
  73.                 try {  
  74.                     shared.produce(i);                    
  75.                 } catch (InterruptedException e) {  
  76.                     e.printStackTrace();  
  77.                 }                     
  78.             }  
  79.         }  
  80.     }  
  81.    
  82.     public static void main(String[] args) throws Exception {  
  83.         SharedData shared = new SharedData();     
  84.            
  85.         Thread producer = new Thread(new ProducerThread(shared),"producer");           
  86.         Thread consumer = new Thread(new ConsumerThread(shared),"consumer");  
  87.           
  88.         consumer.start();  
  89.         producer.start();  
  90.     }  
  91.   
  92. }  


测试输出:

Produced: 0
    Consumed: 0
Produced: 1
    Consumed: 1
Produced: 2
    Consumed: 2
Produced: 3
    Consumed: 3
Produced: 4
    Consumed: 4
Produced: 5
    Consumed: 5
Produced: 6
    Consumed: 6
Produced: 7
    Consumed: 7
Produced: 8
    Consumed: 8
Produced: 9
    Consumed: 9
Produced: 10
    Consumed: 10
Produced: 11
    Consumed: 11
Produced: 12
    Consumed: 12
Produced: 13
    Consumed: 13
Produced: 14
    Consumed: 14
Produced: 15
    Consumed: 15
Produced: 16
    Consumed: 16
Produced: 17
    Consumed: 17
Produced: 18
    Consumed: 18
Produced: 19
    Consumed: 19
Produced: 20
    Consumed: 20
Produced: 21
    Consumed: 21
Produced: 22
    Consumed: 22