DelayQueue是否线程安全

public static void main(String[] args) {
        for(int i = 0; i < 1000;i++){
            TmOppEntity recuritOppEntity = new TmOppEntity(10);
            recuritOppEntity.setAlog("asdasda");
            recuritOppEntity.setIndustry("测试行业" + i);
            recuritOppEntity.setOppId("商机" + i);
            TeleMarketingCallProducer.OPP_ENTITY_QUEUE.offer(recuritOppEntity);
        }

        new Thread(() -> {
            Thread.currentThread().setName("线程1");
            while(true){
                TmOppEntity oppEntity = TeleMarketingCallProducer.OPP_ENTITY_QUEUE.poll();
                if(null != oppEntity){
                    System.out.println(Thread.currentThread().getName() + " 拉取商机:" + oppEntity.getOppId());
                }
            }
        }).start();

        new Thread(() -> {
            Thread.currentThread().setName("线程2");
            while(true){
                TmOppEntity oppEntity = TeleMarketingCallProducer.OPP_ENTITY_QUEUE.poll();
                if(null != oppEntity){
                    System.out.println(Thread.currentThread().getName() + " 拉取商机:" + oppEntity.getOppId());
                }
            }
        }).start();

        new Thread(() -> {
            Thread.currentThread().setName("线程3");
            while(true){
                TmOppEntity oppEntity = TeleMarketingCallProducer.OPP_ENTITY_QUEUE.poll();
                if(null != oppEntity){
                    System.out.println(Thread.currentThread().getName() + " 拉取商机:" + oppEntity.getOppId());
                }
            }
        }).start();

        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

  

跑出数据:

 

商机去重复,未发现重复值,说明一个商机没有被多个线程消费

 

posted on 2022-09-29 17:05  张小泽的小号  阅读(41)  评论(0编辑  收藏  举报

导航