多线程解决生产者/消费者问题

这是本学期的操作系统课程的一个上机实验,用多线程来解决生产者消费者问题,源代码如下,我是初学者,尽量写的浅显些,尽量把注释写的完善些,如果有什么看不懂的可以留言,大家一起学习。

隐藏行号 复制代码 Semaphore.java
  1. public class Semaphore {
    
  2.     int value;
    
  3.     public Semaphore(int v){this.value = v;}
    
  4.     
    
  5.     //定义P原语操作
    
  6.     public synchronized void p(){
    
  7.         value = value-1;
    
  8.         if (value < 0){
    
  9.             try{
    
  10.                 this.wait();
    
  11.             }
    
  12.             catch (InterruptedException e){    
    
  13.             }   
    
  14.         }
    
  15.     }
    
  16. 
    
  17.     
    
  18.     //定义V原语操作
    
  19.     public synchronized void v(){
    
  20.         value = value+1;
    
  21.         if (value <= 0){
    
  22.             this.notify();
    
  23.             }
    
  24.         }
    
  25. }
    
  26.     
    
隐藏行号 复制代码 Producer.java
  1. public class Producer extends Thread{
    
  2.     private Semaphore[] s;
    
  3.     private int i;
    
  4.     public Producer(int i,Semaphore[] sp){
    
  5.     this.i = i;
    
  6.     this.s = sp;
    
  7.     }
    
  8.     public void run() {
    
  9.         while(true){
    
  10.             s[1].p();
    
  11.             s[0].p();
    
  12.             System.out.println("produce "+i++);
    
  13.             try {
    
  14.                 Thread.sleep(30);
    
  15.                 }
    
  16.             catch (InterruptedException e) {}
    
  17.             s[0].v();
    
  18.             s[2].v();
    
  19.             }
    
  20.         }
    
  21.     }
    
隐藏行号 复制代码 Consumer.java
  1. public class Consumer extends Thread {
    
  2.     private Semaphore[] s;
    
  3.     private int i;
    
  4.     public Consumer(int i,Semaphore[] sp){
    
  5.     this.i = i;
    
  6.     this.s = sp;
    
  7.     }
    
  8.     public void run() {    
    
  9.         while(true){
    
  10.             s[2].p();
    
  11.             s[0].p();
    
  12.             System.out.println("consumer "+i++);
    
  13.             try {
    
  14.                 Thread.sleep((long)(Math.random()*100));
    
  15.                 } 
    
  16.             catch (InterruptedException e) {}
    
  17.             s[0].v();
    
  18.             s[1].v();
    
  19.             }
    
  20.         }
    
  21.     }
    
隐藏行号 复制代码 pc.java
  1. public class pc {
    
  2. public static void main(String[] args) {
    
  3.     int mutex = 1;//互斥的信号量,生产者消费者不能同时访问缓冲区
    
  4.     int empty = 3;//表示空闲的缓冲区的数目,初始值为3
    
  5.     int full = 0;//full表示有数据的缓冲区的数目,初始值为0
    
  6.     Semaphore[] s = new Semaphore[3];
    
  7.     s[0] = new Semaphore(mutex);
    
  8.     s[1] = new Semaphore(empty);
    
  9.     s[2] = new Semaphore(full);
    
  10.     Producer p = new Producer(0,s);
    
  11.     Consumer c = new Consumer(0,s);
    
  12.     Thread pp = new Thread(p);
    
  13.     Thread cc = new Thread(c);
    
  14.     Thread pp1 = new Thread(p);
    
  15.     Thread cc1 = new Thread(c);
    
  16.     cc1.start();
    
  17.     pp1.start();
    
  18.     pp.start();
    
  19.     cc.start();
    
  20.     }
    
  21. }
    

 

posted on 2011-05-04 23:10  Kaichd  阅读(229)  评论(0编辑  收藏  举报

导航