生产者消费者问题阻塞队列实现

阻塞队列的核心方法 

 

package InterView;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class MyResources{

    private volatile boolean flag = true; //默认开启进行生产+消费
    private AtomicInteger atomicInteger = new AtomicInteger(); 
    BlockingQueue<String> blockingQueue = null;   

    public MyResources(BlockingQueue<String> blockingQueue)
    {
        this.blockingQueue = blockingQueue;
        //表示使用哪个实现类
        System.out.println(blockingQueue.getClass().getName());
    }


    public void myProd() throws Exception {
        String data = "";
        boolean res;
        while(flag)
        {
            //产生的数据
            data = atomicInteger.incrementAndGet()+"";
            //如果两秒钟没有插入就返回false,表示插入失败
            res = blockingQueue.offer(data, 2, TimeUnit.SECONDS);
            //插入成功
            if(res)
            {
                System.out.println(Thread.currentThread().getName()+"\t 插入队列"+data+"\t成功");
            }else //插入失败
            {
                System.out.println(Thread.currentThread().getName()+"\t 插入队列"+data+"\t失败");
            }
            //一秒生产一个
            TimeUnit.SECONDS.sleep(1);
        }
        System.out.println(Thread.currentThread().getName()+"\t 大老板叫停,生产动作结束");
    }

    public void myConsumer() throws Exception {
        String res = "";
        while(flag)
        {
            //两秒获取不到值返回null,代表队列已空
            res = blockingQueue.poll(2,TimeUnit.SECONDS);
            //说明阻塞队列没有数据了
            if(res == null || "".equals(res))
            {
                flag = false;
                System.out.println(Thread.currentThread().getName()+"\t 超过两秒没有取出数据,消费退出");
                System.out.println();
                return;
            }
            System.out.println(Thread.currentThread().getName()+"\t 消费队列"+res+"\t成功");
        }
    }
    //停止生产+消费动作
    public void stop()
    {
        flag = false;
    }

}

public class ProdConsumer_BlockQueueDemo {


    public static void main(String[] args) {
        //共享资源
        MyResources myResources = new MyResources(new ArrayBlockingQueue<String>(5));
        //生产者线程
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"\t 线程启动");
                myResources.myProd();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"生产者").start();


        //消费者线程
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"\t 线程启动");
                System.out.println();
                System.out.println();
                myResources.myConsumer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"消费者").start();

        //睡眠五秒,五秒以后结束生产+消费的动作
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("五秒时间到,boss叫停,结束");
        //停止生产+消费动作
        myResources.stop();


    }
}
运行结果:

java.util.concurrent.ArrayBlockingQueue
生产者	 线程启动
消费者	 线程启动


生产者	 插入队列1	成功
消费者	 消费队列1	成功
生产者	 插入队列2	成功
消费者	 消费队列2	成功
生产者	 插入队列3	成功
消费者	 消费队列3	成功
生产者	 插入队列4	成功
消费者	 消费队列4	成功
消费者	 消费队列5	成功
生产者	 插入队列5	成功



五秒时间到,boss叫停,结束
生产者	 大老板叫停,生产动作结束
消费者	 超过两秒没有取出数据,消费退出

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值