• Java多线程使用wait和notify实现生产者消费者模型


    Java多线程使用wait和notify这两个关键字的学习,通过实现生成者与消费者来成对研究比较科学。

    从两个字的意义来讲就是等待与通知这个简单道理。

    现在先模拟一个缓存区存储,是用一个list实现的,基本逻辑是当list中数据最大的时候,就等待消费者获取数据并移除数据,然后通知生产者继续生产数据。

    代码如下:

    package com.ming.thread.t3.waitandnotify;
    
    import java.util.Date;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * 先建立一个缓冲区的存储
     * @author mingge
     *
     */
    public class BufferStorage {
    
        private int maxSize=0;
        
        private List<String> list;
        
        public BufferStorage(int maxSize){
            this.maxSize=maxSize;
            list=new LinkedList<>();
        }
        
        synchronized public void set(String str){
            while(list.size()==maxSize){//当list中装的数据最大时,就等待消费者线程
                try {  
                    wait();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }
            list.add(str+":"+new Date().getTime());
             System.out.printf("Set: %d",list.size());  
             System.out.println();
            notifyAll();
        }
        
        synchronized public void get(){
            while(list.size()==0){
                try {  
                    wait();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }
            System.out.printf("Get: %d: %s",list.size(),((LinkedList<?>)list).poll());  //poll 先得到数据,在remove数据
            notifyAll();
        }
        
    }
    package com.ming.thread.t3.waitandnotify;
    
    
    /**
     * 建立一个生产者线程
     * @author mingge
     *
     */
    public class ProducerThread extends Thread{
    
        BufferStorage bufferStorage;
        
        public ProducerThread(BufferStorage bufferStorage){
            super();
            this.bufferStorage=bufferStorage;
            super.setName("ProducerThread");
        }
        
        public void run(){
            for(int i=0;i<200;i++){
                bufferStorage.set("mingge:");
            }
        }
    }
    package com.ming.thread.t3.waitandnotify;
    
    /**
     * 建立一个消费者线程
     * @author mingge
     *
     */
    public class CustomerThread extends Thread{
    
        BufferStorage bufferStorage;
        
        public CustomerThread(BufferStorage bufferStorage){
            super();
            this.bufferStorage=bufferStorage;
            super.setName("CustomerThread");
        }
        
        public void run(){
            for(int i = 0; i < 200; i++) {  
                bufferStorage.get();
            }
        }
        
    }
    package com.ming.thread.t3.waitandnotify;
    
    
    public class Test {
    
        public static void main(String[] args) {
            BufferStorage bufferStorage=new BufferStorage(10);
            ProducerThread producerThread=new ProducerThread(bufferStorage);
            CustomerThread customerThread=new CustomerThread(bufferStorage);
            producerThread.start();
            customerThread.start();
        }
    }

    嗨。。。明白原理就是了......

  • 相关阅读:
    12-转盘
    11-UIView与核心动画对比
    10-动画组
    09-转场动画
    08-图片抖动(帧动画)
    07-心跳效果
    06-CABasicAnimation基础核心动画
    05-时钟效果
    计时器延迟 NSTimer和CADisplaylink GCD中的延迟
    ScrollView与UIPageController
  • 原文地址:https://www.cnblogs.com/huzi007/p/7066100.html
Copyright © 2020-2023  润新知