本文主要介绍了Java 线程池框架的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
一、线程池结构图
二、示例
定义线程接口
public class MyThread extends Thread { @Override publicvoid run() { System.out.println(Thread.currentThread().getName() + "正在执行"); } }
1:newSingleThreadExecutor
ExecutorService pool = Executors. newSingleThreadExecutor(); Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); //将线程放入池中进行执行 pool.execute(t1); pool.execute(t2); pool.execute(t3); //关闭线程池 pool.shutdown();
输入结果:
pool-1-thread-1正在执行 pool-1-thread-1正在执行 pool-1-thread-1正在执行
2:newFixedThreadPool
ExecutorService pool = Executors.newFixedThreadPool(3); Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); Thread t4 = new MyThread(); Thread t5 = new MyThread(); //将线程放入池中进行执行 pool.execute(t1); pool.execute(t2); pool.execute(t3); pool.execute(t4); pool.execute(t5); pool.shutdown();
输入结果:
pool-1-thread-1正在执行 pool-1-thread-2正在执行 pool-1-thread-1正在执行 pool-1-thread-2正在执行
3 :newCachedThreadPool
ExecutorService pool = Executors.newCachedThreadPool(); Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); Thread t4 = new MyThread(); Thread t5 = new MyThread(); //将线程放入池中进行执行 pool.execute(t1); pool.execute(t2); pool.execute(t3); pool.execute(t4); pool.execute(t5); //关闭线程池 pool.shutdown();
输入结果:
pool-1-thread-2正在执行 pool-1-thread-4正在执行 pool-1-thread-3正在执行 pool-1-thread-1正在执行 pool-1-thread-5正在执行
4 :ScheduledThreadPoolExecutor
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); pool.scheduleAtFixedRate(new Runnable() {//每隔一段时间就触发异常 @Override public void run() { //throw new RuntimeException(); System.out.println("================"); } }, 1000, 2000, TimeUnit.MILLISECONDS); pool.scheduleAtFixedRate(new Runnable() {//每隔一段时间打印系统时间,证明两者是互不影响的 @Override public void run() { System.out.println("+++++++++++++++++"); } }, 1000, 2000, TimeUnit.MILLISECONDS);
输入结果:
================ +++++++++++++++++ +++++++++++++++++ +++++++++++++++++
三、线程池核心参数
corePoolSize : 池中核心的线程数
maximumPoolSize : 池中允许的最大线程数。
keepAliveTime : 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
unit : keepAliveTime 参数的时间单位。
workQueue : 执行前用于保持任务的队列。此队列仅保持由 execute方法提交的 Runnable任务。
threadFactory : 执行程序创建新线程时使用的工厂。
handler : 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。
ThreadPoolExecutor :Executors类的底层实现。
3.1 任务排队机制
SynchonousQueue: 同步队列,队列直接提交给线程执行而不保持它们,此时线程池通常是无界的
LinkedBlockingQueue: 无界对列,当线程池线程数达到最大数量时,新任务就会在队列中等待执行,可能会造成队列无限膨胀
ArrayBlockingQueue : 有界队列,有助于防止资源耗尽,一旦达到上限,可能会造成新任务丢失
注意:
newSingleThreadExecutor、newFixedThreadPool使用的是LinkedBlockingQueue
newCachedThreadPool 使用的是 SynchonousQueue
newScheduledThreadPool使用的是 DelayedWorkQueue
3.2 线程执行流程
3.3 线程大小确定:
cpu密集型: 尽量少开线程,最佳线程数 Ncpu+1
io密集型:多开线程,2Ncpu
混合型:根据情况而定,可以拆分成io密集和cou密集
更多Java 线程池框架相关文章请关注PHP中文网!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

WebStorm Mac版
好用的JavaScript开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。