使用ExecutorService有什么优点? [英] What are the advantages of using an ExecutorService?
问题描述
使用 ExecutorService
超过运行线程将 Runnable
传递到线程
构造函数?
What is the advantage of using ExecutorService
over running threads passing a Runnable
into the Thread
constructor?
推荐答案
ExecutorService
消除了与较低级别抽象相关的许多复杂性,例如raw Thread
。它提供了在成功或突然终止任务时安全启动,关闭,提交,执行和阻止的机制(表示为 Runnable
或 Callable
)。
ExecutorService
abstracts away many of the complexities associated with the lower-level abstractions like raw Thread
. It provides mechanisms for safely starting, closing down, submitting, executing, and blocking on the successful or abrupt termination of tasks (expressed as Runnable
or Callable
).
从 JCiP ,第6.2节,直接从马的嘴:
From JCiP, Section 6.2, straight from the horse's mouth:
执行者
可能是一个简单的界面,为一个支持各种任务执行策略的异步任务执行的灵活和强大的框架。它提供了一种标准方法,用于将任务提交与任务执行分离,将任务描述为Runnable
。Executor
实现还提供生命周期支持和钩子,用于添加统计信息收集,应用程序管理和监控。
...
使用Executor
通常是在应用程序中实现生产者 - 消费者设计的最简单路径。
Executor
may be a simple interface, but it forms the basis for a flexible and powerful framework for asynchronous task execution that supports a wide variety of task execution policies. It provides a standard means of decoupling task submission from task execution, describing tasks asRunnable
. TheExecutor
implementations also provide lifecycle support and hooks for adding statistics gathering, application management, and monitoring. ... Using anExecutor
is usually the easiest path to implementing a producer-consumer design in your application.
而不是花时间实现(通常不正确,并付出很大努力)底层基础结构的并行性, juconcurrent
框架允许你集中于结构化任务,依赖性,潜在的并行性。对于大量的并发应用程序,直接识别和利用任务边界并使用 juc
,允许您专注于真正并发挑战的更小的子集可能需要更专业的解决方案。
Rather than spending your time implementing (often incorrectly, and with great effort) the underlying infrastructure for parallelism, the j.u.concurrent
framework allows you to instead focus on structuring tasks, dependencies, potential parallelism. For a large swath of concurrent applications, it is straightforward to identify and exploit task boundaries and make use of j.u.c
, allowing you to focus on the much smaller subset of true concurrency challenges which may require more specialized solutions.
此外,尽管样板的外观和感觉, Oracle API页面总结并发实用程序包括一些真正坚实的参数,使用它们,至少:
Also, despite the boilerplate look and feel, the Oracle API page summarizing the concurrency utilities includes some really solid arguments for using them, not least:
开发人员很可能已经
理解标准库
类,所以没有必要学习
API和ad-hoc的行为
并发组件。此外,
并发应用程序在可靠,测试良好的组件上构建
时,更易于调试
。
Developers are likely to already understand the standard library classes, so there is no need to learn the API and behavior of ad-hoc concurrent components. Additionally, concurrent applications are far simpler to debug when they are built on reliable, well-tested components.
此 SO问题询问一本好书,直接的答案是JCiP。如果你还没有,给自己一份副本。在这个问题上,并发性的综合方法已经远远超越了这个问题,并且从长远来看会为你节省很多心血。
This question on SO asks about a good book, to which the immediate answer is JCiP. If you haven't already, get yourself a copy. The comprehensive approach to concurrency presented there goes well beyond this question, and will save you a lot of heartache in the long run.
这篇关于使用ExecutorService有什么优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!