当前位置:网站首页>Use the executors class to quickly create a thread pool
Use the executors class to quickly create a thread pool
2022-04-23 14:14:00 【pureluckyfish】
Executors Class Executor、ExecutorService、ScheduledExecutorService、ThreadFactory and Callable Class factory methods and utility methods . This class supports the following methods :
One 、 Introduction to methods in this class
1. Static methods for creating various thread pools , The return value is ExecutorService
| Method | describe |
| newCachedThreadPool() | Create a cache thread pool , Threads in the thread pool will survive 60s |
| newCachedThreadPool(ThreadFactory threadFactory) | Use the specified factory class , Create a cache thread pool , Threads in the thread pool will survive 60s |
| newFixedThreadPool(int nThreads) | Create a thread pool with a fixed number of threads |
| newFixedThreadPool(int nThreads, ThreadFactory threadFactory) | Use the specified factory class , Create a thread pool with a fixed number of threads |
| newSingleThreadExecutor() | Create a thread pool of threads |
| newSingleThreadExecutor(ThreadFactory threadFactory) | Use the specified factory class , Create a thread pool of threads |
2. Static methods for creating various thread pools , The return value is ScheduledExecutorService
| Method | describe |
| newScheduledThreadPool(int corePoolSize) | Create a thread pool , The thread pool can schedule commands to run after a given delay or execute periodically |
| newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) | Use the specified factory class , Create a thread pool , The thread pool can schedule commands to run after a given delay or execute periodically |
| newSingleThreadScheduledExecutor() | Create a single threaded executor , The actuator can schedule commands to run after a given delay or execute them regularly |
| newSingleThreadScheduledExecutor(ThreadFactory threadFactory) | Use the specified factory class , Create a single threaded executor , The actuator can schedule commands to run after a given delay or execute them regularly |
3. Create and return one ThreadFactory, The ThreadFactory Set the newly created thread to a known state
| Return value | Method | describe |
| static ThreadFactory | defaultThreadFactory() | Return a default thread factory class to create a new thread |
| static ThreadFactory | privilegedThreadFactory() | Returns a thread factory , This thread factory is used to create a new thread with the same permissions as the current thread ,privilegedThreadFactory Class inherited defaultThreadFactory class |
4. Create and return a from other closure like forms Callable Methods , Therefore, they can be used to need Callable How to execute
| Return value | Method | describe |
| static Callable<Object> | callable(PrivilegedAction<?> action) | Return to one Callable object , The object runs the given privileged operation and returns its result when called |
| static Callable<Object> | callable(PrivilegedExceptionAction<?> action) | Return to one Callable object , The object runs the given privileged exception operation and returns its result when called |
| static Callable<Object> | Return to one Callable object , This object runs the given task when called and returns null | |
| static <T> Callable<T> | callable(Runnable task, T result) | Return to one Callable object , This object runs the given task and returns the given result when called |
Two 、 Not recommended Executors Why
Thread pools are not allowed Executors To create , But through ThreadPoolExecutor The way , This way of processing makes the students who write more clear about the running rules of the thread pool , Avoid the risk of resource exhaustion .Executors The disadvantages of each method :
1. newSingleThreadExecutor and newFixedThreadPool: The main problem is that the docked request processing queue may consume a lot of memory , even to the extent that OOM
2.newCachedThreadPool and newScheduledThreadPool: The main problem is that the maximum number of threads is Integer.MAX_VALUE, A very large number of threads may be created , Set up OOM
3、 ... and 、Executor Interface
Only one in the interface execute Method , This interface is the ancestor of all thread pools
版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406486515.html
边栏推荐
猜你喜欢

MySQL数据库讲解(七)

多云数据流转?云上容灾?年前最后的价值内容分享

HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能

win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件

利用json-server在本地创建服务器请求

Gartner预测云迁移规模大幅增长;云迁移的优势是什么?

使用Executors类快速创建线程池

MySQL数据库讲解(十)

政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移

MYSQL一种分表实现方案及InnoDB、MyISAM、MRG_MYISAM等各种引擎应用场景介绍
随机推荐
JDBC详解
Wechat applet communicates with esp8266 based on UDP protocol
星界边境Starbound创意工坊订阅的mod的存放路径
关于密匙传递的安全性和数字签名
RecyclerView高级使用(二)-垂直拖拽排序的简单实现
RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复
mysql 5.1升级到5.66
Homebrew是什么?以及使用
连接公司跳板机取别名
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
dp-能量项链
容灾有疑问?点这里
Recyclerview advanced use (I) - simple implementation of sideslip deletion
MySQL数据库讲解(七)
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
Jmeter设置环境变量支持在任意终端目录输入jmeter直接启动
操作系统常见面试题目:
Mysql个人学习总结
Can global variables be defined in header files
Pass in external parameters to the main function in clion