远征有限 2019-04-01 17:37
浏览 154

关于使用阻塞队列的技术问题,请大家帮我看看有什么问题?

具体代码如下:

package com.service;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class Test {

    private ArrayBlockingQueue<String> blockQueue = null;
    private boolean befinesh = false;
    public Test() {



    }
    public static void main(String[] args) {
        new Test().test();
    }

    public void test() {
        this.blockQueue = new ArrayBlockingQueue<String>(10,true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("开始-----");
                    do {
                        System.out.println(Thread.currentThread().getId() + ":" + blockQueue.poll(10,TimeUnit.SECONDS));
                        Thread.sleep(1000L);    
                    } while (!blockQueue.isEmpty()||befinesh == false);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        for (int i =0; i < 30; i ++) {
            try {
//              Thread.sleep(9000L);
                String str = "kjhhk_" + i;
                blockQueue.put(str);
                System.out.println(Thread.currentThread().getId() + ":往queue中放入" + str);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.befinesh = true;
    }

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度