简体   繁体   English

ThreadPoolExecutor:它如何重用线程

[英]ThreadPoolExecutor: how does it reuse threads

I read that ThreadPoolExecutor has pool of threads and this pool is destined to reduce cost of creation new threads (at least I understood phrase below in such way): 我读到ThreadPoolExecutor有线程池,这个池注定要降低创建新线程的成本(至少我以这种方式理解下面的短语):

When you send a task to the executor, it tries to use a pooled thread for the execution of this task, to avoid continious spawning of threads. 当您将任务发送到执行程序时,它会尝试使用池化线程来执行此任务,以避免持续产生线程。 [Java 7 Concurrency Cookbook] [Java 7并发手册]

BUT , as I know we have got no way to restart thread in Java. 但是 ,据我所知,我们无法在Java中重启线程。

Question: How does ThreadPoolExecutor avoid creation of new threads? 问题: ThreadPoolExecutor如何避免创建新线程?

It's very simple - in essence the Thread s sleep, waiting to be woken by a task - they run that task and then sleep again. 它非常简单 - 本质上是Thread的睡眠,等待被任务唤醒 - 它们运行该任务然后再次睡眠。

public static void main(final String[] args) throws Exception {
    final BlockingQueue<Runnable> blockingQueue = new LinkedBlockingDeque<>();
    final Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try {
                    blockingQueue.take().run();
                } catch (InterruptedException ex) {
                    return;
                }
            }
        }
    });
    t.start();
    blockingQueue.add(new Runnable() {

        @Override
        public void run() {
            System.out.println("Task 1");
        }
    });
    blockingQueue.add(new Runnable() {

        @Override
        public void run() {
            System.out.println("Task 2");
        }
    });
}

The BlockingQueue will block a Thread while it is empty. BlockingQueue将阻止一个Thread ,同时它是空的。 When I add an item, and Thread (s) currently blocked are awoken and one will take the task ( LinkedBlockingDeque is thread safe). 当我添加一个项目时,当前被阻止的Thread被唤醒,并且将执行任务( LinkedBlockingDeque是线程安全的)。 When the Thread is done with the task it goes back to sleep. Thread完成任务后,它会重新进入休眠状态。

The JavaDoc for ThreadPoolExecutor describes the logic in detail. ThreadPoolExecutorJavaDoc详细描述了逻辑。 All of the constructors for ThreadPoolExecutor take a BlockingQueue<Runnable> - this should give you a hint as so how the logic works. ThreadPoolExecutor所有构造函数都采用BlockingQueue<Runnable> - 这应该给出一个提示,因为逻辑是如何工作的。

NB: This is not the same as busy waiting . 注意: 这与忙碌的等待不一样 A BlockingQueue uses wait and notify to suspend and wake Thread s, this means that the Thread s in the pool are not doing any work when they are not processing tasks. BlockingQueue使用waitnotify来挂起和唤醒Thread ,这意味着池中的Thread在没有处理任务时没有做任何工作。 A busy wait based approach would not work because the Thread s would block up all the CPU cores with their polling not allowing the program to proceed (or at least severely impairing it). 基于繁忙等待的方法不起作用,因为Thread s将阻止所有CPU核心,其轮询不允许程序继续(或至少严重损害它)。

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

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