Java BlockingQueue take()vs poll(时间,单位)

时间:2015-09-28 19:48:01

标签: java multithreading queue blockingqueue

我认为错误的人比较了take()vs poll(),但我发现将take()与poll(时间,单位)进行比较是比较合理的,因为BlockingQueue提供的两者都阻塞告诉队列而不是空“在案件或民意调查或超时“,OK让我们开始比较,通常我使用take()for BlockingQueue,但我遇到了以下问题:

  1. 处理循环内部的中断。
  2. 等到被外界打断。
  3. 如何使用Kill-Bill或中断线程停止在队列上循环“
  4. 特别是在使用Java 8流时,我知道我需要停止从队列中检索数据并以更好的方式关闭它,所以我想等待一段时间后我可以停止检索数据然后我发现了民意调查(时间,单位),它适合下面这个想法检查代码:

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> q = new LinkedBlockingQueue<Integer>();
        ExecutorService executor = Executors.newCachedThreadPool();
        executor.submit(() -> {
            IntStream.range(0, 1000).boxed().forEach(i -> {
                try {
                    q.put(i);
                } catch (InterruptedException e) {
                    currentThread().interrupt();
                    throw new RuntimeException(e);
                }
            });
        });
        ....
        // Take
        Future fTake = executor.submit(() -> {
    
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(q.take());
                }
            } catch (InterruptedException e) {
                currentThread().interrupt();
                throw new RuntimeException(e);
            }
    
        });
        //to stop it I have to do below code "Expecting that execution will take 1 sec"
        executor.shutdown();
        sleep(1000);
        fTake.cancel(true);
    
        ....
        // poll there is no need to expect time till processing will be done
        Future fPoll = executor.submit(() -> {
            try {
                Integer i;
                while ((i = q.poll(100, TimeUnit.MILLISECONDS)) != null)
                    System.out.println(i);
            } catch (InterruptedException e) {
                currentThread().interrupt();
                throw new RuntimeException(e);
            }
        });
        executor.shutdown();
    }
    

    我认为轮询代码更干净,不需要依赖中断,也不需要估计执行时间或者让代码确定何时中断线程,您怎么看?

    注1:我确信第二种解决方案也有缺点,比如没有数据直到超时,但我想你会知道什么是合适的超时时间。

    注2:如果用例需要等待,生产者是低频提供数据,我认为采取解决方案更好。

0 个答案:

没有答案