Java Executor服务线程池 [英] Java Executor Service Thread Pool

查看:117
本文介绍了Java Executor服务线程池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用Executor框架在java中创建一个包含10个线程的固定大小的线程池:

If I create a fixed size thread pool with 10 threads in java using Executor framework:

private final ExecutorService pool;
pool = Executors.newFixedThreadPool(10);

然后尝试提交10个以上的任务(例如12个任务);

and then try to submit more than 10 tasks (say for an example, 12 tasks);

for (int i = 0 ; i < 12 ; i++) {
    pool.execute(new Handler(myRunnable));
}

额外任务会发生什么(额外的两个任务,例如12个任务)?它们是否会被阻塞直到线程完成其工作?

What will happen to the extra tasks (extra two tasks as per the example of 12 tasks)? Will they be blocked until a thread has finished its work?

推荐答案

引用Javadoc Executors.newFixedThreadPool (int nThreads)强调我的):


创建一个线程池重用在共享的无界队列中运行的固定数量的线程。在任何时候,最多nThreads线程将是活动的处理任务。 如果在所有线程都处于活动状态时提交了其他任务,则它们将在队列中等待,直到线程可用。如果任何线程由于在关闭之前执行期间的故障而终止,则如果需要执行后续任务,则新线程将取代它。池中的线程将一直存在,直到它被显式关闭。

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

与Java并发框架一样成熟的代码的Javadoc包含一个很棒的代码丰富的知识。保持关闭。

The Javadocs for code as mature as the Java Concurrency Framework contain a great wealth of knowledge. Keep them close.

这篇关于Java Executor服务线程池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆