当前位置: 首页 > news >正文

网站编辑教程什么是网站外链

网站编辑教程,什么是网站外链,湖南企业做网站,锦江网站建设【线程与线程池】线程数设置 0. 全局使用一个线程池业务中使用优雅关闭线程池(如在应用退出时)另一种方法 1. 按照任务类型对线程池进行分类1.1 Netty的IO处理任务,就是典型的IO密集型任务 2. 混合型任务创建线程池时,如何确定线程…

【线程与线程池】线程数设置

  • 0. 全局使用一个线程池
    • 业务中使用
    • 优雅关闭线程池(如在应用退出时)
    • 另一种方法
  • 1. 按照任务类型对线程池进行分类
    • 1.1 Netty的IO处理任务,就是典型的IO密集型任务
  • 2. 混合型任务创建线程池时,如何确定线程数

0. 全局使用一个线程池

import java.util.concurrent.*;public class GlobalThreadPool {// 单例线程池private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(10,                      // 核心线程数50,                      // 最大线程数60L, TimeUnit.SECONDS,   // 空闲线程最大存活时间new LinkedBlockingQueue<>(1000), // 工作队列new ThreadFactory() {private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();private int counter = 0;@Overridepublic Thread newThread(Runnable r) {Thread thread = defaultFactory.newThread(r);thread.setName("global-thread-" + counter++);return thread;}},new ThreadPoolExecutor.AbortPolicy()  // 拒绝策略);private GlobalThreadPool() {// 私有构造防止实例化}public static ThreadPoolExecutor getExecutor() {return EXECUTOR;}
}

业务中使用

import java.util.concurrent.Future;public class Demo {public void runTask() {Runnable task = () -> {System.out.println("执行任务:" + Thread.currentThread().getName());};// 提交任务GlobalThreadPool.getExecutor().execute(task);}
}

也可以使用 submit() 获取 Future 对象:

Future<String> future = GlobalThreadPool.getExecutor().submit(() -> {// 业务逻辑return "result";
});

优雅关闭线程池(如在应用退出时)

public class ShutdownHook {public static void register() {Runtime.getRuntime().addShutdownHook(new Thread(() -> {System.out.println("关闭全局线程池...");GlobalThreadPool.getExecutor().shutdown();try {if (!GlobalThreadPool.getExecutor().awaitTermination(60, TimeUnit.SECONDS)) {GlobalThreadPool.getExecutor().shutdownNow();}} catch (InterruptedException e) {GlobalThreadPool.getExecutor().shutdownNow();}}));}
}Spring Boot 中可以在 @PostConstruct 中调用注册:@Component
public class AppInitializer {@PostConstructpublic void init() {ShutdownHook.register();}
}

另一种方法

Spring Boot 中,更建议使用 Spring@Bean + 注入方式统一线程池,例如:import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.annotation.PreDestroy;
import java.util.concurrent.*;@Configuration
public class ThreadPoolConfig {@Value("${threadpool.corePoolSize:10}")private int corePoolSize;@Value("${threadpool.maxPoolSize:50}")private int maxPoolSize;@Value("${threadpool.queueCapacity:1000}")private int queueCapacity;@Value("${threadpool.keepAliveSeconds:60}")private long keepAliveSeconds;@Value("${threadpool.threadNamePrefix:global-pool}")private String threadNamePrefix;@Value("${threadpool.rejectedPolicy:CallerRunsPolicy}")private String rejectedPolicy;private ThreadPoolExecutor executor;@Bean(name = "globalExecutor", destroyMethod = "") // 禁用 Spring 自动销毁,手动控制public Executor globalExecutor() {executor = new ThreadPoolExecutor(corePoolSize,maxPoolSize,keepAliveSeconds, TimeUnit.SECONDS,new LinkedBlockingQueue<>(queueCapacity),new CustomThreadFactory(threadNamePrefix),getRejectedExecutionHandler());return executor;}@PreDestroypublic void shutdown() {if (executor != null) {System.out.println("[ThreadPoolConfig] 正在关闭线程池...");executor.shutdown();try {if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {System.out.println("[ThreadPoolConfig] 超时未关闭,强制关闭线程池");executor.shutdownNow();}} catch (InterruptedException e) {executor.shutdownNow();Thread.currentThread().interrupt();System.out.println("[ThreadPoolConfig] 线程池关闭被中断,已强制关闭");}System.out.println("[ThreadPoolConfig] 线程池已成功关闭");}}private RejectedExecutionHandler getRejectedExecutionHandler() {switch (rejectedPolicy) {case "AbortPolicy":return new ThreadPoolExecutor.AbortPolicy();case "DiscardPolicy":return new ThreadPoolExecutor.DiscardPolicy();case "DiscardOldestPolicy":return new ThreadPoolExecutor.DiscardOldestPolicy();case "CallerRunsPolicy":default:return new ThreadPoolExecutor.CallerRunsPolicy();}}private static class CustomThreadFactory implements ThreadFactory {private final String prefix;private final ThreadGroup group;private int count = 1;CustomThreadFactory(String prefix) {this.prefix = prefix;this.group = Thread.currentThread().getThreadGroup();}@Overridepublic Thread newThread(Runnable r) {Thread t = new Thread(group, r, prefix + "-thread-" + count++);t.setDaemon(false);t.setPriority(Thread.NORM_PRIORITY);return t;}}
}

使用方式:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.concurrent.Executor;@Service
public class TaskService {private final Executor globalExecutor;@Autowiredpublic TaskService(@Qualifier("globalExecutor") Executor globalExecutor) {this.globalExecutor = globalExecutor;}public void submitTasks() {for (int i = 1; i <= 5; i++) {int taskId = i;globalExecutor.execute(() -> {System.out.println(Thread.currentThread().getName() + " 正在执行任务: " + taskId);try {Thread.sleep(2000);  // 模拟任务执行耗时} catch (InterruptedException e) {Thread.currentThread().interrupt();}System.out.println(Thread.currentThread().getName() + " 完成任务: " + taskId);});}}
}@Qualifier("globalExecutor") 的作用是指定注入哪个具体的 Bean。是否必须加它,取决于你项目中是否存在多个同类型的 Bean

启动入口调用(例如在 Spring Boot 主类或测试里)

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class Runner implements CommandLineRunner {private final TaskService taskService;public Runner(TaskService taskService) {this.taskService = taskService;}@Overridepublic void run(String... args) throws Exception {System.out.println("提交任务到全局线程池...");taskService.submitTasks();}
}

1. 按照任务类型对线程池进行分类

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.1 Netty的IO处理任务,就是典型的IO密集型任务

在这里插入图片描述
在这里插入图片描述

2. 混合型任务创建线程池时,如何确定线程数

在这里插入图片描述

P10

http://www.khdw.cn/news/55226.html

相关文章:

  • 网站设计 优帮云新软件推广
  • 免费商标图案设计logo网站seo推广哪家值得信赖
  • wordpress 多功能主题seo优化关键词排名
  • 有没有做微场景的网站女教师遭网课入侵直播录屏曝光se
  • 护卫神做的网站访问友情链接查询
  • 代做课程设计的网站网络推广引流方式
  • 广德网站建设营销计划
  • 物流企业网站建设关键词在线试听
  • 网站建设公司招人灰色广告投放平台
  • 江苏又一地出现疫情淘宝优化
  • 韩雪冬网站设计长沙网络推广
  • 用自己主机做网站淘宝关键词排名优化技巧
  • 东莞网站建设服务公司没经验怎么开广告公司
  • 个人网站建设流程谷歌sem推广
  • 做网站云服务器2m宽带够用吗网络广告的概念
  • 赚钱网站建设北京做网站的公司有哪些
  • 浪漫做爰网站直接下载app
  • 我的网站首页打不开深圳整站全网推广
  • 动态网站建设从入门到精通 下载一站式发稿平台
  • 常见的静态网站开发技术色目人
  • 靠谱营销网站开发选哪家网站seo在线诊断
  • 网站开发 需求外贸网站建设案例
  • 辽宁省工程造价管理总站佛山seo网站排名
  • 做网站设计制作的google play三件套
  • 佛山外贸网站建设报价发帖推广哪个平台好
  • 做打折网站如何合肥搜索引擎优化
  • 响应式网站建设代理外贸国际网站推广
  • 原网站开发新功能燃灯seo
  • 南京佛搜做网站公司seochinaz查询
  • 考研培训百度seo怎么收费