【翻译二十】-java线程池

Thread Pools

Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks.

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. 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. 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.

A simple way to create an executor that uses a fixed thread pool is to invoke the newFixedThreadPool factory method injava.util.concurrent.Executors This class also provides the following factory methods:

  • The newCachedThreadPool method creates an executor with an expandable thread pool. This executor is suitable for applications that launch many short-lived tasks.
  • The newSingleThreadExecutor method creates an executor that executes a single task at a time.
  • Several factory methods are ScheduledExecutorService versions of the above executors.

If none of the executors provided by the above factory methods meet your needs, constructing instances ofjava.util.concurrent.ThreadPoolExecutor or java.util.concurrent.ScheduledThreadPoolExecutor will give you additional options.


译文:

线程池
  许多在java.util.concurrent中的执行器方法都是使用的线程池,它组成了工作线程。这种线程单独存在于Runnable和Callable任务中,通常用来执行多个任务。
  使用工作线程,最大限度的减少由于创建线程的开销。线程对象需要使用大量的内存,并且在大规模应用程序中,分配和回收大量线程对象也带来大量的内存开销。
  一种通用的线程池是fixed thread pool。这种线程池通常有一个指定数字的线程个数运行。如果一个线程是在它正在使用的时候被终止了,它会自动被一个新的线程所替代。任务通过线程的内部队列提交到线程池,它将会保持额外的任务当有比线程更多的活动任务的时候。

  对于fixed thread pool的一个非常大的优势是应用程序用它减少优雅。为了理解这个意思,考虑一个网络服务器程序它的每个Http请求都会持有一个分开的线程。如果这个程序简单的创建一个新的线程给每个新的http请求,并且这个系统会获得比它立刻能处理的更多的请求,这个程序会突然停止响应所有的请求当所有的线程超过系统的容积时。由于创建线程的个数可以限制,这个程序可以不立即去处理Http请求,但是可以在系统允许的最快的时间内去响应。

  一个简单的用fixed thread pool创建执行器的方法是执行在java.util.concurrent.Executors包中的newFixedThreadPool工厂方法。这个类也提供如下的工厂方法:

  • newCachedThreadPool方法创建一个有可扩展的线程池的执行器。这个执行器适合需要载入许多短生命任务的应用程序。
  • newSingleThreadExecutor方法创建一个执行器能一次执行一个任务。
  • 许多工厂方法是ScheduledExecutorService版在之前提到过的。

  如果以上工厂提供的方法不能满足你需求,构造java.util.concurrent.ThreadPoolExecutor或者java.util.concurrent.ScheduledThreadPoolExecutor实例可以给你更多选择。

你可能感兴趣的:(java线程池)