【翻译二十】-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
newCachedThreadPoolmethod creates an executor with an expandable thread pool. This executor is suitable for applications that launch many short-lived tasks. - The
newSingleThreadExecutormethod creates an executor that executes a single task at a time. - Several factory methods are
ScheduledExecutorServiceversions 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线程池的更多相关文章
- 并发编程(十二)—— Java 线程池 实现原理与源码深度解析 之 submit 方法 (二)
在上一篇<并发编程(十一)—— Java 线程池 实现原理与源码深度解析(一)>中提到了线程池ThreadPoolExecutor的原理以及它的execute方法.这篇文章是接着上一篇文章 ...
- Java多线程和并发(十二),Java线程池
目录 1.利用Executors创建线程的五种不同方式 2.为什么要使用线程池 3.Executor的框架 4.J.U.C的三个Executor接口 5.ThreadPoolExecutor 6.线程 ...
- Java线程池详解
一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...
- 【java线程系列】java线程系列之java线程池详解
一线程池的概念及为何需要线程池: 我们知道当我们自己创建一个线程时如果该线程执行完任务后就进入死亡状态,这样如果我们需要在次使用一个线程时得重新创建一个线程,但是线程的创建是要付出一定的代价的,如果在 ...
- Java线程池学习心得
一.普通线程和线程池的对比 new Thread的弊端如下: a. 每次new Thread新建对象性能差.b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或o ...
- Java线程池详解(一)
一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...
- Java线程池使用和分析(二) - execute()原理
相关文章目录: Java线程池使用和分析(一) Java线程池使用和分析(二) - execute()原理 execute()是 java.util.concurrent.Executor接口中唯一的 ...
- java线程池技术(二): 核心ThreadPoolExecutor介绍
版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程池技术属于比较"古老"而又比较基础的技术了,本篇博客主要作用是个人技术梳理,没什么新玩意. 一.Java线程池技术的 ...
- Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理
相关文章目录: Java线程池ThreadPoolExecutor使用和分析(一) Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理 Java线程池Thr ...
- Java线程池详解(二)
一.前言 在总结了线程池的一些原理及实现细节之后,产出了一篇文章:Java线程池详解(一),后面的(一)是在本文出现之后加上的,而本文就成了(二).因为在写完第一篇关于java线程池的文章之后,越发觉 ...
随机推荐
- Mean Shift Tracking: 2000-2012回顾 (新论文更新)
参考: Mean Shift Tracking: 2000-2012回顾 (新论文更新) ECCV2016要来了,估计深度学习要一统天下了吧
- BZOJ 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松
Description 给你一个图,两个点至多有一条路径,求最长的一条路径. \(n \leqslant 4\times 10^4\) Sol DFS?DP? 这就是一棵树,方向什么的都没用... 然 ...
- Python自动化之socketserver模块
1 动态导入模块 import importlib aa = importlib.import_module("lib1.aa") //lib跟当前模块不是一个目录,aa是lib下 ...
- sharepoint 2010 页面刷新时滚动条位置保持不变 Controlling scrollbar position on postback
sharepoint 2010 页面刷新时滚动条位置保持不变 Controlling scrollbar position on postback在sharepoint 2010中,如果当前页面的篇幅 ...
- sizeof进行结构体大小的判断
typedef struct{ int a; char b;}A_t;typedef struct{ int a; char b; char c;}B_t;typedef ...
- wxpython 基本的控件 (按钮)
使用按钮工作 在wxPython 中有很多不同类型的按钮.这一节,我们将讨论文本按钮.位图按钮.开关按钮(toggle buttons )和通用(generic )按钮. 如何生成一个按钮? 在第一部 ...
- 基础02 Java 跨平台原理
1993 , JAVA初衷: 机顶盒 1994 年互联网刚刚兴起,.(高司令\ 高斯林),改造成了面向互联网的计算机语言.java重要特性之 ------- 跨平台(一次编译,到处运行).平台:操作系 ...
- 6.nodejs权威指南--进程
1. 进程 var net = require('net'); var cluster = require('cluster'); cluster.setupMaster({ exec:'child. ...
- jQuery 调用jsonp实现与原理
jQuery 调用jsonp实现与原理 您的评价: 收藏该经验 阅读目录 1.客户端代码 2.服务器端 通过jQuery实现JSONP 一般的ajax是不能跨域请求的,因此需要使 ...
- Python:IDLE清屏
清屏很简单,为IDLE增加一个清屏的扩展ClearWindow即可. 首先下载clearwindow.py(点击可直接下载,不能下载的可以右键保存,格式为py结尾), 将这个文件放到Python安装目 ...