简体   繁体   English

Java调度的线程池执行器内部队列

[英]Java scheduled threadpool executor internal queue

I want to write a program that runs every 30 minutes. 我想编写一个每30分钟运行一次的程序。 I am using java scheduled threadpool executor to process the tasks that i submit to the executor. 我正在使用Java调度的线程池执行程序来处理我提交给执行程序的任务。

I have been looking at what the official docs say https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html 我一直在看官方文档怎么说https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html

and i have run into a dilemma. 我陷入了困境。

Lets say i have submitted 5 tasks to the queue and i have defined 10 threads in the thread pool. 可以说我已经向队列提交了5个任务,并且我在线程池中定义了10个线程。

  1. Is there a likelyhood that one of the tasks shall be performed twice 是否有可能其中一项任务应执行两次

  2. Does the threadpool executor make sure that a task is removed when it has been processed by one of the threads or must i remove the task myself once it has been processed. 线程池执行程序是否确保在某个线程处理完任务后将其删除,或者一旦处理完任务后我是否必须自己删除该任务。

Having the task removed is desirable since i wouldn't like old tasks to still be in the queue 30 minutes later. 删除任务是可取的,因为我不希望老任务在30分钟后仍然在队列中。

It will be executed only once, the executor will remove it automatically. 它只会执行一次,执行者会自动将其删除。

This is not explicitly documented, while the doc implys it: 这没有明确记录,而文档暗示了:

Executes the given task sometime in the future. 在将来的某个时间执行给定的任务。

Executors.newFixedThreadPool() creates a new ThreadPoolExecutor using a LinkedBlockingQueue . Executors.newFixedThreadPool()使用LinkedBlockingQueue创建新的ThreadPoolExecutor

From Executors.newFixedThreadPool() : 来自Executors.newFixedThreadPool()

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

When tasks are submitted for executions, the ThreadPoolExecutor will add them to this queue. 当任务被提交执行时, ThreadPoolExecutor会将它们添加到此队列中。 Further, the worker threads will take tasks from this queue and execute them. 此外,工作线程将从该队列中获取任务并执行它们。

From ThreadPoolExecutor.getTask() : ThreadPoolExecutor.getTask()

private Runnable getTask() {
  // ...
        try {
            Runnable r = timed ?
                workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                workQueue.take();
            if (r != null)
                return r;
            timedOut = true;
        } catch (InterruptedException retry) {
            timedOut = false;
        }
    }
}

As per BlockingQueue.take() contract, taking an element from the queue implies removing it as well. 根据BlockingQueue.take()协定,从队列中取出一个元素也意味着将其删除。

/**
 * Retrieves and removes the head of this queue, waiting if necessary
 * until an element becomes available.
 *
 * @return the head of this queue
 * @throws InterruptedException if interrupted while waiting
 */
E take() throws InterruptedException;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM