Java SE入门及基础(62) 线程池 执行器

embedded/2024/10/21 11:55:40/

线程池

1. 执行器

        In all of the previous examples, there's a close connection between the task being done by a new thread, as defined by its Runnable object, and the thread itself, as defined by a Thread object. This works well for small applications, but in large-scale applications, it makes sense to separate thread management and creation from the rest of the application. Objects that encapsulate these functions are known as executors.
        在前面的所有示例中,由新线程(由其Runnable 对象定义)执行的任务与由 Thread 对象定义的线程本身之间存在紧密的联系。 这对于小型应用程序非常有效,但是在大型应用程序中,将线程管理和创建与其余应用程序分开是有意义的。 封装这些功能的对象称为执行器。
Executor接口方法
void execute ( Runnable command ); // 将任务添加到线程池中,等待线程池调度执行
ExecutorService接口常用方法
void shutdown (); // 有序关闭线程池,不再接收新的线程任务,但池中已有任务会执行
List < Runnable > shutdownNow (); // 关闭线程池,尝试停止所有正在执行的任务,并将池中等待执行的任务返回
boolean isShutdown (); // 检测线程池是否已经关闭
boolean isTerminated (); // 检测线程池是否已经终止
Future <?> submit ( Runnable task ); // 提交一个任务至线程池中

2.线程池

        Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks.
        java.util.concurrent中的大多数执行程序实现都使用线程池,该线程池由工作线程组成。这种线程与它执行的Runnable Callable 任务分开存在,通常用于执行多个任务。
        Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead.
        使用工作线程可以最大程度地减少线程创建所带来的开销。线程对象占用大量内存,在大型应用程序中,分配和取消分配许多线程对象会产生大量内存管理开销。
        One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread. Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads.
        线程池的一种常见类型是固定线程池。这种类型的池始终具有指定数量的正在运行的线程。如果某个线程在仍在使用时以某种方式终止,则它将自动替换为新线程。任务通过内部队列提交到池中,该内部队列在活动任务多于线程时容纳额外的任务。
        An important advantage of the fixed thread pool is that applications using it degrade gracefully.
        固定线程池的一个重要优点是使用该线程池的应用程序可以正常降级
线程池构造方法
public ThreadPoolExecutor ( int corePoolSize , // 核心线程数
int maximumPoolSize , // 最大线程数
long keepAliveTime , // 工作线程存活时间
TimeUnit unit , // 时间单位
BlockingQueue < Runnable > workQueue , // 任务队列
ThreadFactory threadFactory , // 线程工厂
RejectedExecutionHandler handler ) // 拒绝处理器
示例
import java.util.Queue;
import java.util.concurrent.*;
public class ThreadPoolTest {public static void main(String[] args) {LinkedBlockingDeque<Runnable> taskQueue = new LinkedBlockingDeque<>(10);ThreadPoolExecutor pool = new ThreadPoolExecutor(5, //核心线程数10, //最大工作线程数2,//非核心线程的工作线程存活时间TimeUnit.SECONDS,//存活时间单位taskQueue,//任务队列Executors.defaultThreadFactory(),//线程池中的线程创建工厂new ThreadPoolExecutor.AbortPolicy());//拒绝新线程任务的策略for(int i=0; i<30; i++){pool.submit(new ThreadPoolTask(i));int corePoolSize = pool.getCorePoolSize();//获取核心线程数int size = pool.getQueue().size(); //获取队列中任务个数long finish = pool.getCompletedTaskCount();//获取线程池执行完成任务的个数System.out.printf("线程池中核心线程数:%d,队列中任务个数:%d,线程池完成任务数:%d\n",corePoolSize, size, finish);try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}}pool.shutdown();//关闭线程池,等待线程池中的任务执行完成,但是不会接收新的线程任务}static class ThreadPoolTask implements Runnable{private int num;public ThreadPoolTask(int num) {this.num = num;}@Overridepublic void run() {System.out.println("正在执行线程任务" + num);try {Thread.sleep(400);} catch (InterruptedException e) {e.printStackTrace();}}}
}
线程池工作流程
        线程池启动后,核心线程就已经启动,当一个新的任务提交到线程池时,首先会检测当前是否存在空闲的核心线程,如果存在,就将该任务交给这个空闲核心线程执行。如果不存在,那么就将该任务交给队列,在队列中排队等候。如果队列满了,此时线程池会检测当前工作线程数是否达到最大线程数,如果没有达到最大线程数,那么将由线程工厂创建新的工作线程来执行队列中的任务,这样,队列中就有空间能够容纳这个新任务。如果创建的工作线程在执行完任务后,在给定的时间范围内没有新的任务执行,这些工作线程将死亡。如果已经达到最大线程数,那么线程池将采用提供的拒绝处理策略来拒绝这个新任务。
线程池创建方式
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorTest {public static void main(String[] args) {
//创建一个给定核心线程数以及最大线程数的线程池,该线程池队列非常大ExecutorService pool1 = Executors.newFixedThreadPool(5);
//创建只有一个核心线程数以及最大线程数的线程池,该线程池队列非常大ExecutorService pool2 = Executors.newSingleThreadExecutor();
//创建一个核心线程为0,最大线程数为整数的最大值的可缓存的线程池ExecutorService pool3 = Executors.newCachedThreadPool();
//创建一个给定核心线程数,最大线程数为整数的最大值的可调度的线程池ExecutorService pool4 = Executors.newScheduledThreadPool(5);}
}

3. 线程池的使用

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ExecutorTaskTest {public static void main(String[] args) {ExecutorService service = Executors.newFixedThreadPool(5);for (int i = 0; i < 100; i++) {int order = i;service.submit(() -> System.out.println("正在执行任务" + order));}service.shutdown();}
}

http://www.ppmy.cn/embedded/56065.html

相关文章

AI 与 Python 实战干货:基于深度学习的图像识别

《AI 与 Python 实战干货&#xff1a;基于深度学习的图像识别》 今天咱不啰嗦&#xff0c;直接上干货&#xff01; 在 AI 领域&#xff0c;特别是图像识别方面&#xff0c;Python 简直是一把利器。咱就以手写数字识别为例&#xff0c;来看看怎么用 Python 实现一个深度学习模…

【mybatis】mybatis-plus简单使用

1、简介 MyBatis-Plus&#xff08;简称MP&#xff09;是MyBatis的增强工具&#xff0c;它在MyBatis的基础上进行了扩展&#xff0c;以简化开发、提高效率。本案例是一个MyBatis-Plus的简单使用案例&#xff0c;包括项目的初始化、配置、实体类、Mapper接口以及基本的CRUD操作。…

3ds Max导出fbx贴图问题简单记录

1.前言 工作中发现3ds Max导出的fbx在其它软件&#xff08;Autodesk viewer&#xff0c;blender&#xff0c;navisworks&#xff0c;FBXReview等&#xff09;中丢失了部分贴图&#xff0c;但导出的fbx用3ds Max打开却正常显示。 fbx格式使用范围较广&#xff0c;很多常见的三…

整合 Mybatis Plus

什么是 MyBatis Plus&#xff1f; MyBatis Plus &#xff08;简称 MP&#xff09; 是一款持久层框架&#xff0c;说白话就是一款操作数据库的框架。它是一个 MyBatis 的增强工具&#xff0c;就像 iPhone手机一般都有个 plus 版本一样&#xff0c;它在 MyBatis 的基础上只做增强…

Elasticsearch环境搭建|ES单机|ES单节点模式启动|ES集群搭建|ES集群环境搭建

文章目录 版本选择单机ES安装与配置创建非root用户导入安装包安装包解压配置JDK环境变量配置single-node配置JVM参数后台启动|启动日志查看启动成功&#xff0c;访问终端访问浏览器访问 Kibana安装修改配置后台启动|启动日志查看浏览器访问 ES三节点集群搭建停止es服务域名配置…

如何免费封装APP:小猪APP分发的终极指南

你是否曾经因为APP的封装过程而头疼不已&#xff1f;让我们来聊聊如何在不花一分钱的情况下完成这一任务。而且&#xff0c;我们还会介绍一个超级实用的工具——小猪APP分发&#xff0c;让你轻松解决封装APP的难题。 小猪app封装www.ppzhu.net 什么是APP封装&#xff1f; APP…

Python基础入门知识

目录 引言 简要介绍Python语言 为什么要学习Python Python的应用领域 Python安装和环境配置 Python的下载和安装(Windows, macOS, Linux) 配置Python环境变量 安装和使用IDE(如PyCharm, VS Code) Python基本语法 注释 变量和数据类型(数字,字符串,列表,元组,字典,…

考研计算机网络(第二章 物理层2奈氏准则和香农定理)

目录 1.失真 2.奈氏准则&#xff08;奈奎斯特定理&#xff09; 3.香农定理 1.失真 影响失真程度的因素&#xff1a;码元传输速率&#xff08;速率高&#xff0c;失真程度高&#xff09;&#xff0c;信号传输距离&#xff08;距离远&#xff0c;失真程度高&#xff09;&#…