• 多线程之生产者--消费者


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

    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();
                }
            }
        }
        
    }
  • 相关阅读:
    linux下mysql的安装
    linux下mysql设置主从
    linux下安装jdk8并且配置环境变量
    C#实现rabbitmq 延迟队列功能
    对angular.js的一点理解
    angular.js的路由和模板在asp.net mvc 中的使用
    通过Web Api 和 Angular.js 构建单页面的web 程序
    Orchard运用
    Orchard运用
    Orchard运用
  • 原文地址:https://www.cnblogs.com/sloveling/p/producter_customer.html
Copyright © 2020-2023  润新知