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();
                }
            }
        }
        
    }
  • 相关阅读:
    Node(十一)mongoose配合Node实现注册登录(注册上传头像,登录成功后显示用户信息)
    JS案例:购物车操作(简单实现)
    JS案例:Jq中的fadeOut和fadeIn实现简单轮播(没完善,简单实现)
    JS案例:小球拖动,记录轨迹,并原路返回
    html框架frame iframe
    单元测试
    软件测试计划、依据、规范
    软件测试
    html表单
    html块 布局
  • 原文地址:https://www.cnblogs.com/sloveling/p/producter_customer.html
Copyright © 2011-2022 走看看