zoukankan      html  css  js  c++  java
  • 多线程之生产者--消费者

    别的不多说,直接代码上:

    package cn.thread.lock;
    //主方法
    public class ProducerCustomer {
        public static void main(String[] args) {
            Pool pool=new Pool();
            Customer c=new Customer(pool);
            Producer p=new Producer(pool);
            new Thread(c).start();
            new Thread(p).start();
            
            
        }
    }
    //产品类
    class Product{
        int id;
        public Product(int id) {
            this.id=id;
        }
        @Override
        public String toString() {
            
            return "product:"+id;
        }
    }
    //池类
    class Pool{
        private int index=0;
        private Product[] products=new Product[5];
        //生产
        public synchronized void push(Product p){
            while(index==products.length){//池子满了
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notify();
            products[index]=p;
            this.index++;
        }
        //消费
        public synchronized Product pop(){
            while(index==0){//池子空了
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
            }
            this.notify();
            index--;
            return products[index];
        }
        
        
    }
    //消费者
    class Customer implements Runnable{
        private Pool pool=null;
        
        Customer(Pool p){
            this.pool=p;
        }
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                Product product = pool.pop();
                System.out.println("消费了:"+product);
            }
            try {
                Thread.sleep(1000);//消费一个产品,休眠1秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            
        }
        
    }
    //生产者
    class Producer implements Runnable{
    private Pool pool=null;
        
        Producer(Pool p){
            this.pool=p;
        }
        @Override
        public void run() {
            
            for (int i = 0; i < 20; i++) {
                Product product = new Product(i);
                pool.push(product);
                System.out.println("生产了产品:"+product);
                try {
                    Thread.sleep(1000);//生产一个产品,休息1秒钟
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
  • 相关阅读:
    gRPC java 客户端,服务器端通讯使用json格式
    HDTV(1920x1080)码率和视频质量关系的研究 2 (实验结果)
    Fedora 18/19没有注销
    window API一天一练之邮槽
    在C语言环境下使用google protobuf
    Cantor展开式
    LeetCode题解:Rotate List
    不知不觉vs2012 update 4出来了
    http://download.csdn.net/detail/yanzi1225627/6548337
    【虚拟化实战】Cluster设计之一资源池
  • 原文地址:https://www.cnblogs.com/sloveling/p/producter_customer.html
Copyright © 2011-2022 走看看