固定线程池和预定线程池之间的Java差异
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了固定线程池和预定线程池之间的Java差异,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1331字,纯文字阅读大概需要2分钟。
内容图文
![固定线程池和预定线程池之间的Java差异](/upload/InfoBanner/zyjiaocheng/823/7de5479e919243b9ad17bbaf2fb08cda.jpg)
我有一个固定的线程池,可以随时运行7个并发线程(带队列),我想把它变成一个只运行7个并发作业的调度线程池,但可以排队/安排更多.
阅读文档并没有真正帮助我..
public static ExecutorService newFixedThreadPool(int nThreads)
Creates a thread pool that reuses a fixed set of threads operating off
a shared unbounded queue. If any thread terminates due to a failure
during execution prior to shutdown, a new one will take its place if
needed to execute subsequent tasks.Parameters:
nThreads – the number of threads in the pool Returns:
the newly created thread pool
public static ScheduledExecutorService newScheduledThreadPool(int
corePoolSize)Creates a thread pool that can schedule commands to run after a given
delay, or to execute periodically.Parameters:
corePoolSize – the number of threads to keep in the pool, even if they are idle. Returns:
a newly created scheduled thread pool
我不明白的是,corePoolSize和nThreads是一回事吗?调度线程池是否真的是固定线程池的一个子集,这意味着我可以将调度线程池用作可以对延迟任务进行排队的固定线程池?
解决方法:
是的,它们基本上是相同的,只是增加了调度功能. ScheduledThreadPoolExecutor甚至扩展了ExecutorService(ThreadPoolExecutor)的默认实现.
nThreads和corePoolSize是要生成的线程数.对于固定的执行者,它始终是相同的.对于其他实现,它在min(corePoolSize)和max(maxPoolSize)之间变化.
内容总结
以上是互联网集市为您收集整理的固定线程池和预定线程池之间的Java差异全部内容,希望文章能够帮你解决固定线程池和预定线程池之间的Java差异所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。