当前位置:网站首页>线程池的创建
线程池的创建
2022-06-10 21:50:00 【InfoQ】
线程池的创建
创建一个线程池时需要输入几个参数,如下。
1)corePoolSize(线程池的基本大小):当提交一个任务到线程池时,线程池会创建一个线程来执行任务,即使其他空闲的基本线程能够执行新任务也会创建线程,等到需要执行的任务数大于线程池基本大小时就不再创建。如果调用了线程池的prestartAllCoreThreads()方法,线程池会提前创建并启动所有基本线程。
2)runnableTaskQueue(任务队列):用于保存等待执行的任务的阻塞队列。可以选择以下几个阻塞队列。
- ArrayBlockingQueue:是一个基于数组结构的有界阻塞队列,此队列按FIFO(先进先出)原则对元素进行排序。
- LinkedBlockingQueue:一个基于链表结构的阻塞队列,此队列按FIFO排序元素,吞吐量通常要高于ArrayBlockingQueue。静态工厂方法Executors.newFixedThreadPool()使用了这个队列。
- SynchronousQueue:一个不存储元素的阻塞队列。每个插入操作必须等到另一个线程调用移除操作,否则插入操作一直处于阻塞状态,吞吐量通常要高于
- Linked-BlockingQueue,静态工厂方法Executors.newCachedThreadPool使用了这个队列。
- PriorityBlockingQueue:一个具有优先级的无限阻塞队列。
3)maximumPoolSize(线程池最大数量):线程池允许创建的最大线程数。如果队列满了,并且已创建的线程数小于最大线程数,则线程池会再创建新的线程执行任务。值得注意的是,如果使用了无界的任务队列这个参数就没什么效果。
4)ThreadFactory:用于设置创建线程的工厂,可以通过线程工厂给每个创建出来的线程设置更有意义的名字。使用开源框架guava提供的ThreadFactoryBuilder可以快速给线程池里的线程设置有意义的名字,代码如下。
```java
new ThreadFactoryBuilder().setNameFormat("XX-task-%d").build();
- AbortPolicy:直接抛出异常。
- CallerRunsPolicy:只用调用者所在线程来运行任务。
- DiscardOldestPolicy:丢弃队列里最近的一个任务,并执行当前任务。
- DiscardPolicy:不处理,丢弃掉。
边栏推荐
- Informatics Olympiad all in one 1280: [example 9.24] skiing | openjudge noi 2.690: skiing | Luogu p1434 [show2002] skiing
- MySQL相关-0416
- A journey of database full SQL analysis and audit system performance optimization
- MySQL master-slave replication solves read-write separation
- Reading notes on the way of Huawei data
- 玩电子,穷三代
- That's great. The Ministry of industry and information technology has launched an internet account with a "one click unbinding" mobile phone number, which can be called an artifact
- [original] analysis of nine price HPV data capture of Yilu app
- Face recognition software based on deepface model
- Missing heritability
猜你喜欢

数字孪生:第三人称鼠标操作
![[original] analysis of nine price HPV data capture of Yilu app](/img/f2/c792367beea0956fe69aad5d35581a.png)
[original] analysis of nine price HPV data capture of Yilu app

Auto. JS Pro development environment configuration

Storage engine analysis

Display of successful cases of target customer matching data table
![[applet] vant sliding cell adds the function of clicking other positions to close automatically](/img/ef/3d954323a981026e2b917428cbc92b.png)
[applet] vant sliding cell adds the function of clicking other positions to close automatically

Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事

Web3生态去中心化金融平台——Sealem Finance

The definition of the metauniverse and the seven infinite features

Html+php+mysql login registration page
随机推荐
Operation of simulated examination platform for welder (primary) test questions in 2022
smb匿名
leetcode 130. Surrounded regions (medium)
Sealem finance builds Web3 decentralized financial platform infrastructure
重排 (reflow) 与重绘 (repaint)
数字孪生:第三人称鼠标操作
玩电子,穷三代
JS sensitive information leak detection tool
How about opening an account with flush? Is it safe to open an account?
1.Tornado简介&&本专栏搭建tornado项目简介
Sentinel
Native support for the first version of arm64! Microsoft win11/10 free tool set PowerToys 0.59 release
汇编:汇编与C派系语言混用以及对应LLDB常用指令
How small and micro enterprises build micro official websites at low cost
Auto. JS Pro development environment configuration
聚簇索引和非聚簇索引
Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事
[play with Huawei cloud] take you through the Kunpeng code migration tool to realize source code migration
Digital twin: third person mouse operation
盲盒商城APP系统开发的软件特点和盲盒功能介绍