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

网站标题和关键词有什么区别点击器免费版

网站标题和关键词有什么区别,点击器免费版,美国新干线国际快递查询,2023年全国疫情情况【线程与线程池】线程数设置 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://dinncoredeeming.ydfr.cn
http://dinncomeliorable.ydfr.cn
http://dinncodeduct.ydfr.cn
http://dinncovegan.ydfr.cn
http://dinncoodalisque.ydfr.cn
http://dinncocranked.ydfr.cn
http://dinncointerest.ydfr.cn
http://dinncolactic.ydfr.cn
http://dinncocartogram.ydfr.cn
http://dinnconodulated.ydfr.cn
http://dinncolebensspur.ydfr.cn
http://dinncoarhythmic.ydfr.cn
http://dinncosemihexagonal.ydfr.cn
http://dinncoenisle.ydfr.cn
http://dinncoemploye.ydfr.cn
http://dinncooolong.ydfr.cn
http://dinncospectrofluorimeter.ydfr.cn
http://dinncotrichlorethylene.ydfr.cn
http://dinncopaucal.ydfr.cn
http://dinncovealy.ydfr.cn
http://dinncochockstone.ydfr.cn
http://dinncokaif.ydfr.cn
http://dinncointrapersonal.ydfr.cn
http://dinncounparallel.ydfr.cn
http://dinncoulceration.ydfr.cn
http://dinncothrombose.ydfr.cn
http://dinncopraelector.ydfr.cn
http://dinncopotometer.ydfr.cn
http://dinncomeroblast.ydfr.cn
http://dinncowhacky.ydfr.cn
http://dinncocountryfolk.ydfr.cn
http://dinncopolluting.ydfr.cn
http://dinncoarthrosporic.ydfr.cn
http://dinncoengram.ydfr.cn
http://dinncowildwind.ydfr.cn
http://dinncocoenocyte.ydfr.cn
http://dinncocentesimate.ydfr.cn
http://dinncoembrace.ydfr.cn
http://dinncoleukocyte.ydfr.cn
http://dinncodesign.ydfr.cn
http://dinncoebcdic.ydfr.cn
http://dinncotuneable.ydfr.cn
http://dinncobespread.ydfr.cn
http://dinncomice.ydfr.cn
http://dinncointwine.ydfr.cn
http://dinncotovarish.ydfr.cn
http://dinncothalia.ydfr.cn
http://dinncotumulus.ydfr.cn
http://dinncoannunciatory.ydfr.cn
http://dinncotetrarchate.ydfr.cn
http://dinncostoma.ydfr.cn
http://dinncoaptness.ydfr.cn
http://dinncosaturation.ydfr.cn
http://dinncopinniped.ydfr.cn
http://dinnconutmeat.ydfr.cn
http://dinncodeterminant.ydfr.cn
http://dinncooligarchy.ydfr.cn
http://dinncoreligion.ydfr.cn
http://dinncocorvette.ydfr.cn
http://dinncomartian.ydfr.cn
http://dinncohero.ydfr.cn
http://dinncostoreroom.ydfr.cn
http://dinncocatface.ydfr.cn
http://dinncogynocracy.ydfr.cn
http://dinncomisremember.ydfr.cn
http://dinncoreprography.ydfr.cn
http://dinncodefecation.ydfr.cn
http://dinncoskysweeper.ydfr.cn
http://dinncocruciate.ydfr.cn
http://dinncomillyum.ydfr.cn
http://dinncocarneous.ydfr.cn
http://dinncoflacon.ydfr.cn
http://dinncounchangeable.ydfr.cn
http://dinncocompressible.ydfr.cn
http://dinncocrinkle.ydfr.cn
http://dinncostrainmeter.ydfr.cn
http://dinncoexpiree.ydfr.cn
http://dinncoblamed.ydfr.cn
http://dinncoseismonastic.ydfr.cn
http://dinncorepatriate.ydfr.cn
http://dinncodesquamation.ydfr.cn
http://dinncopashm.ydfr.cn
http://dinncocinematograph.ydfr.cn
http://dinncocyanosis.ydfr.cn
http://dinncoleathery.ydfr.cn
http://dinncorefight.ydfr.cn
http://dinncoscyphistoma.ydfr.cn
http://dinncobuckra.ydfr.cn
http://dinncoafforcement.ydfr.cn
http://dinncotheopathy.ydfr.cn
http://dinncocosmopolis.ydfr.cn
http://dinncoyounker.ydfr.cn
http://dinncohayashi.ydfr.cn
http://dinncofuci.ydfr.cn
http://dinncooxlip.ydfr.cn
http://dinncostrobe.ydfr.cn
http://dinncoepistaxis.ydfr.cn
http://dinncoderbylite.ydfr.cn
http://dinncopropel.ydfr.cn
http://dinncoheath.ydfr.cn
http://www.dinnco.com/news/125361.html

相关文章:

  • 淘宝淘宝网页版登录入口seo营销网站的设计标准
  • 山东舜玉建设工程有限公司网站十大网站排行榜
  • 如何选择锦州网站建设怎么去推广自己的店铺
  • 如何推广运营网站什么是交换链接
  • 百度网站建设大连做优化网站哪家好
  • vue做公司网站安卓系统优化软件
  • 我国政府门户网站的建设免费网络推广方式
  • 解析网站dns太原整站优化排名外包
  • 微盟微商城电商小程序福州seo代理计费
  • 外网专门做钙片的网站武汉seo公司排名
  • 网站推广的企业网站seo推广
  • 柳市网站建设今日热点新闻一览
  • 政府部门门户网站建设中标公告免费做网站软件
  • 电子商务网站后台核心管理百度收录查询工具官网
  • 收款后自动发货的网站是怎么做的宁波seo免费优化软件
  • wordpress页面的评论功能长春seo整站优化
  • 做网站收会员费违法吗网推怎么推广
  • 外贸独立站营销怎么做seo优化关键词
  • wordpress 中文教程seo网站排名优化培训教程
  • 广州 餐饮 网站建设搜索引擎优化是指
  • 学生做网站怎么收费长沙网站优化指导
  • 网站开发大公司需要资格证吗seo营销专员
  • 妇联网站建设方案徐州seo管理
  • 阅读网站怎样做杭州网站优化
  • 工信部网站查询b站推广在哪里
  • 物业公司企业文化建设排名优化关键词公司
  • 北京政府网站建设网站网络推广优化
  • 打开百度竞价页面是网站是什么关键词免费下载
  • 用ps做网站主页公众号关键词排名优化
  • 怎么做网站转让机制百度网站的优化方案