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

本实验通过Java多线程技术实现生产者消费者问题的解决方案,利用信号量进行资源同步,确保生产者和消费者之间的正确交互。实验中定义了生产者、消费者及信号量类,模拟了生产消费过程。

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

隐藏行号 复制代码 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. }
    

 

转载于:https://www.cnblogs.com/kaichd/archive/2011/05/04/2037083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值