当前位置:网站首页>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
边栏推荐
- Some good articles on pthread multithreading
- Visio installation error 1:1935 2: {XXXXXXXX
- RecyclerView高级使用(二)-垂直拖拽排序的简单实现
- mysql 5.1升级到5.66
- ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
- On September 8, the night before going to Songshan Lake
- squid代理
- 帆软中根据分类进行汇总
- Logback logger and root
- HyperMotion云迁移完成阿里云专有云产品生态集成认证
猜你喜欢

帆软中单元格中隔行变色以及数量大于100字体变大变红设置

Installation and use of postman pit

01-nio basic ByteBuffer and filechannel

星界边境Starbound创意工坊订阅的mod的存放路径

How QT designer adds resource files

Logback logger and root

postman批量生产body信息(实现批量修改数据)

字节面试编程题:最小的K个数

01-NIO基础之ByteBuffer和FileChannel

Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
随机推荐
RecyclerView高级使用(一)-侧滑删除的简单实现
gzip和gunzip 解压参数详解
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
线程间控制之Semaphore使用介绍
OpenStack命令操作
Homebrew是什么?以及使用
How to do a project easily
postman批量生产body信息(实现批量修改数据)
dp-能量项链
百度图片识别自定义实现(替代AipOcr)
关于云容灾,你需要知道这些
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
PySide2
dp-[NOIP2000]方格取数
mysql查询库大小
VMware installation 64 bit XP Chinese tutorial
How does void * exist?
Jmeter安装教程以及我遇到的问题的解决办法
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
连接公司跳板机取别名
