The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Most of the executor implementations in java.util.concurrent
use thread pools, which consist of worker threads. java.util.concurrent
中的大多数执行器实现都使用线程池,线程池由工作线程组成。This kind of thread exists separately from the 这种线程与它执行的Runnable
and Callable
tasks it executes and is often used to execute multiple tasks.Runnable
和Callable
任务分开存在,通常用于执行多个任务。
Using worker threads minimizes the overhead due to thread creation. 使用工作线程可以最大限度地减少由于创建线程而产生的开销。Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead.线程对象使用大量内存,在大型应用程序中,分配和取消分配许多线程对象会产生大量内存管理开销。
One common type of thread pool is the fixed thread pool. 一种常见的线程池类型是固定线程池。This type of pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread. 这种类型的池总是有指定数量的线程在运行;如果线程在仍在使用时以某种方式终止,它将自动替换为新线程。Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads.任务通过内部队列提交到池中,当活动任务多于线程时,内部队列会保存额外的任务。
An important advantage of the fixed thread pool is that applications using it degrade gracefully. 固定线程池的一个重要优点是,使用它的应用程序可以正常降级。To understand this, consider a web server application where each HTTP request is handled by a separate thread. 为了理解这一点,考虑一个Web服务器应用程序,其中每个HTTP请求由一个单独的线程处理。If the application simply creates a new thread for every new HTTP request, and the system receives more requests than it can handle immediately, the application will suddenly stop responding to all requests when the overhead of all those threads exceed the capacity of the system. 如果应用程序只是为每个新HTTP请求创建一个新线程,并且系统接收到的请求超过了它可以立即处理的数量,那么当所有这些线程的开销超过系统的容量时,应用程序将突然停止响应所有请求。With a limit on the number of the threads that can be created, the application will not be servicing HTTP requests as quickly as they come in, but it will be servicing them as quickly as the system can sustain.由于可以创建的线程数量有限,应用程序将不会在HTTP请求传入时尽快为其提供服务,但会在系统能够支持时尽快为其提供服务。
A simple way to create an executor that uses a fixed thread pool is to invoke the 创建使用固定线程池的执行器的简单方法是调用newFixedThreadPool
factory method in java.util.concurrent.Executors
. java.util.concurrent.Executors
中的newFixedThreadPool
工厂方法。This class also provides the following factory methods:此类还提供以下工厂方法:
newCachedThreadPool
method creates an executor with an expandable thread pool. newCachedThreadPool
方法使用可扩展的线程池创建执行器。newSingleThreadExecutor
method creates an executor that executes a single task at a time.newSingleThreadExecutor
方法创建一个一次执行单个任务的执行器。ScheduledExecutorService
versions of the above executors.ScheduledExecutorService
版本。If none of the executors provided by the above factory methods meet your needs, constructing instances of 如果上述工厂方法提供的执行器都不满足您的需要,那么构建java.util.concurrent.ThreadPoolExecutor
or java.util.concurrent.ScheduledThreadPoolExecutor
will give you additional options.java.util.concurrent.ThreadPoolExecutor
或java.util.concurrent.ScheduledThreadPoolExecutor
的实例将为您提供其他选项。