测试往LinkedBlockingQueue放入几个相同的对象

        在项目上使用的时候,关于LinkedBlockingQueue的使用,有一些疑问,就想着测试一下。

1.用同一个String对象,赋不同的值再一一放进去队列中

    try {
            BlockingQueue queue = new LinkedBlockingQueue();
            String content = "content A";
            queue.put(content);
            content = "content B";
            queue.put(content);
            content = "content C";
            queue.put(content);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while(true) {
                            if (queue.size() <= 0) {
                                break;
                            }
                            String str = (String) queue.take();
                            System.out.println(str);
                        }
                    } catch (Exception e) {
                        Log4j2Utils.getinstance().error("", e);
                    }
                }
            }).start();
        } catch (Exception e) {
            Log4j2Utils.getinstance().error("", e);
        }

        测试结果:

content A
content B
content C

        结论:

        从队列中拿出来的是不同的值。

2.新建一个对象,里面只有一个String,然后再使用同一个对象,赋不同的值,再拿出来看看

     static class ContentDetail {
        private String title;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

    public static void main(String[] args) {
        try {
            BlockingQueue queue = new LinkedBlockingQueue();
            ContentDetail contentDetail = new ContentDetail();
            contentDetail.setTitle("content title A");
            queue.put(contentDetail);
            contentDetail.setTitle("content title B");
            queue.put(contentDetail);
            contentDetail.setTitle("content title C");
            queue.put(contentDetail);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while(true) {
                            if (queue.size() <= 0) {
                                break;
                            }
                            ContentDetail contentDetail = (ContentDetail) queue.take();
                            System.out.println(contentDetail.getTitle());
                        }
                    } catch (Exception e) {
                        Log4j2Utils.getinstance().error("", e);
                    }
                }
            }).start();
        } catch (Exception e) {
            Log4j2Utils.getinstance().error("", e);
        }
    }

        测试结果:

content title C
content title C
content title C

        结论:

        要是同一对象的话,以最后一次put进来的值为准

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值