生产者和消费者

阻塞队列+原子类+volatile

package threadTest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; //阻塞队列
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; //原子类
class ShareResource{
    private volatile boolean flag = true ; //默认生产
    private AtomicInteger atomicInteger = new AtomicInteger();
    BlockingQueue<String> blockingQueue = null;

    public ShareResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        System.out.println(blockingQueue.getClass().getName());
    }
    public void myProduct() throws Exception {
        String data = null;
        boolean returnValue;
        while(flag) {
            data = atomicInteger.incrementAndGet() + "";
            returnValue = blockingQueue.offer(data,2L, TimeUnit.SECONDS);
            if(returnValue) {
                System.out.println(Thread.currentThread().getName() + "插入队列成功:" + data);
            }else{
                System.out.println(Thread.currentThread().getName() + "插入队列失败:" + data);
            }
            TimeUnit.SECONDS.sleep(1);
        }
        System.out.println(Thread. currentThread().getName() + "大老板叫停,flag = false,生产线程结束");
    }
    public void myConsume() throws Exception {
        String res = null;
        boolean consumeFlag = true;
        while(flag || consumeFlag){
            res = blockingQueue.poll(2L, TimeUnit.SECONDS);
            if(null == res || res.equalsIgnoreCase("")){
                consumeFlag = false;
                System.out.println(Thread.currentThread().getName() + "超过两秒没有取到,消费退出");
                System.out.println();
                System.out.println();
                continue;
            }
            System.out.println(Thread.currentThread().getName() + "消费队列成功:" + res);
        }
    }
    public void stop() throws Exception{
        this.flag = false;
    }
}
public class ProducterAndConsumer {
    public static void main(String[] args) throws Exception{
        ShareResource shareResource = new ShareResource( new ArrayBlockingQueue<>(3));
         new Thread(() -> {
             System.out.println(Thread.currentThread().getName() + "生产线程启动");
             try {
                 shareResource.myProduct();
             }catch (Exception e){
                 e.printStackTrace();
             }
         },"Prod").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "消费线程启动");
            try {
                shareResource.myConsume();
            }catch (Exception e){
                e.printStackTrace();
            }
        },"Cons").start();

        try {
            TimeUnit.SECONDS.sleep(5);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println();
        System.out.println();
        System.out.println();

        System.out.println("5s,大老板main线程叫停,活动结束");
        shareResource.stop();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值