美文网首页
生产者消费者模型

生产者消费者模型

作者: juexin | 来源:发表于2018-01-17 17:59 被阅读0次

wait/notify实现

package zq;

public class Test { 
    private static Integer count = 0; 
    private final Integer FULL = 5; 
    private static String lock = "lock"; 
 
    public static void main(String[] args) { 
        Test t = new Test(); 
        new Thread(t.new Producer()).start(); 
        new Thread(t.new Consumer()).start(); 
/*      new Thread(t.new Producer()).start(); 
        new Thread(t.new Consumer()).start(); 
        new Thread(t.new Producer()).start(); 
        new Thread(t.new Producer()).start(); */
    } 
 
    class Producer implements Runnable { 
        @Override 
        public void run() { 
            for (int i = 0; i < 10; i++) { 
                try { 
                    Thread.sleep(100); 
                } catch (InterruptedException e1) {  
                    e1.printStackTrace(); 
                } 
                synchronized (lock) { 
                    while (count == FULL) { 
                        try { 
                            System.out.println("生产者-wait");
                            lock.wait(); 
                        } catch (InterruptedException e) {  
                            e.printStackTrace(); 
                        } 
                    } 
                    count++; 
                    System.out.println("生产者"+Thread.currentThread().getName() 
                            + "已生产完成,商品数量:" + count); 
                    lock.notifyAll(); 
                } 
            } 
        } 
    } 
 
    class Consumer implements Runnable { 
        @Override 
        public void run() { 
            for (int i = 0; i < 10; i++) { 
                try { 
                    Thread.sleep(100); 
                } catch (InterruptedException e1) { 
                    e1.printStackTrace(); 
                } 
                synchronized (lock) { 
                    while (count == 0) { 
                        try { 
                            System.out.println("消费者-wait");
                            lock.wait(); 
                        } catch (InterruptedException e) { 
                            e.printStackTrace(); 
                        } 
                    } 
                    count--; 
                    System.out.println("消费者"+Thread.currentThread().getName() 
                            + "已消费,剩余商品数量:" + count); 
                    lock.notifyAll(); 
                } 
            } 
        } 
    } 
}

BlockingQueue实现

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class ProducerConsumerPattern {
 
    public static void main(String args[]){
 
     //Creating shared object
     BlockingQueue sharedQueue = new LinkedBlockingQueue();
 
     //Creating Producer and Consumer Thread
     Thread prodThread = new Thread(new Producer(sharedQueue));
     Thread consThread = new Thread(new Consumer(sharedQueue));
 
     //Starting producer and Consumer thread
     prodThread.start();
     consThread.start();
    }
 
}
 
//Producer Class in java
class Producer implements Runnable {
 
    private final BlockingQueue sharedQueue;
 
    public Producer(BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }
 
    @Override
    public void run() {
        for(int i=0; i<10; i++){
            try {
                System.out.println("Produced: " + i);
                sharedQueue.put(i);
            } catch (InterruptedException ex) {
                Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
 
}
 
//Consumer Class in Java
class Consumer implements Runnable{
 
    private final BlockingQueue sharedQueue;
 
    public Consumer (BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }
 
    @Override
    public void run() {
        while(true){
            try {
                System.out.println("Consumed: "+ sharedQueue.take());
            } catch (InterruptedException ex) {
                Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
 
}

相关文章

  • 34.Python之生产者消费者模型

    Python之生产者消费者模型(非常重要) 生产者消费者模型模型指的是一种解决问题的套路。 生产者消费者模型中包含...

  • 生产者和消费者模型

    生产者和消费者模型 1. 什么是生产者和消费者模型 生产者消费者模型具体来讲,就是在一个系统中,存在生产者和消费者...

  • 生产者消费者(一)

    生产者消费者模型: 生产者------> 缓存<-------- 消费者

  • Future

    Future 模式只是生产者-消费者模型的扩展。经典“生产者-消费者”模型中消息的生产者不关心消费者何时处理完该条...

  • python入门开发学习笔记之掌握什么是生产者消费者模型

    本节重点 熟练掌握什么是生产者消费者模型熟练掌握为什么要用生产者消费者模型熟练掌握如何实现生产者消费者模型本节时长...

  • wait/notify实现生产者消费者(6)

    生产者消费者模型 生产者消费者模型是一个典型的多线程问题,涉及生产者、消费者、产品仓库。生产者生产的产品放入仓库中...

  • 生产者消费者

    生产者/消费者模式(阻塞队列) 生产者消费者模型的实现

  • 生产者消费者模型示例

    生产者消费者模型Main provider(生产者) Consumer(消费者) Data数据 log信息

  • 生产者消费者模型Java实现

    生产者消费者模型 生产者消费者模型可以描述为:①生产者持续生产,直到仓库放满产品,则停止生产进入等待状态;仓库不满...

  • 生产者与消费者模型

    生产者与消费者模型 通过使用Object的wait(),notify()方法进行生产者与消费者模型中出现的数据同步...

网友评论

      本文标题:生产者消费者模型

      本文链接:https://www.haomeiwen.com/subject/ecbuoxtx.html