前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >阻塞队列(新版)消费者生产者

阻塞队列(新版)消费者生产者

作者头像
名字是乱打的
发布2022-05-13 12:28:46
1790
发布2022-05-13 12:28:46
举报
文章被收录于专栏:软件工程

定义好生产者和消费者之后,交给阻塞队列,阻塞队列自己控制生产和消费

代码语言:javascript
复制
package jucTest;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class MyResource{
    public  volatile  boolean flag=true;//定义一个开关,开启生产和消费
    private AtomicInteger atomicInteger=new AtomicInteger();

    BlockingQueue<String> blockingQueue=null;

    public MyResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        System.out.println(blockingQueue.getClass().getName());
    }

    //定义生产者
     public void MyProduct() throws InterruptedException {
        String date=null;
        boolean result;
        while (flag)
        {
            date = atomicInteger.incrementAndGet()+"";
            result = blockingQueue.offer(date, 2L, TimeUnit.SECONDS);
            if (result==true){
                System.out.println(Thread.currentThread().getName()+" 生产"+String.valueOf(date)+" 成功");
            }else{
                System.out.println(Thread.currentThread().getName()+" 生产"+String.valueOf(date)+" 失败");
            }
            TimeUnit.SECONDS.sleep(1);
        }
        System.out.println("老板不让干了,生产结束");
     }
     //定义消费者
    public void  MyCustomer() throws Exception{
        String poll=null;
        while (flag){
            poll= blockingQueue.poll(2L, TimeUnit.SECONDS);
            if (poll==null||poll.equalsIgnoreCase("")){
                System.out.println(" 消费时间超过2S,退出");
                System.out.println();
                System.out.println();
                return;
            }else {
                System.out.println("消费"+String.valueOf(poll)+"成功");
            }
            TimeUnit.SECONDS.sleep(1);
        }
         System.out.println("消费结束");
    }




}
public class test2 {
    public static void main(String[] args){
        MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));

        new Thread(()->{
        System.out.println("生产线程启动");
            try {
                myResource.MyProduct();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"producer").start();

        new Thread(()->{
        System.out.println("消费者线程启动");
            try {
                myResource.MyCustomer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"customer").start();

        //暂停线程
        try{ TimeUnit.SECONDS.sleep(10);} catch(InterruptedException e){ e.printStackTrace();}
        System.out.println("10秒时间到,老板不让干了,下班了");
        myResource.flag=false;

    }
}

控制台打印

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档