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

网站死链接是什么google chrome官网下载

网站死链接是什么,google chrome官网下载,网络推广都有哪些渠道,做学校网站的目的【线程与线程池】线程数设置 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://dinncogiant.ydfr.cn
http://dinncosynthetical.ydfr.cn
http://dinncopetite.ydfr.cn
http://dinncosuperloo.ydfr.cn
http://dinncotimeslice.ydfr.cn
http://dinncodacoity.ydfr.cn
http://dinncomorcellate.ydfr.cn
http://dinncoenvelop.ydfr.cn
http://dinncoenstatite.ydfr.cn
http://dinncopapyraceous.ydfr.cn
http://dinncohempseed.ydfr.cn
http://dinncocercus.ydfr.cn
http://dinncoairmark.ydfr.cn
http://dinncohearting.ydfr.cn
http://dinncotitus.ydfr.cn
http://dinncoincantation.ydfr.cn
http://dinncotallboy.ydfr.cn
http://dinncobiting.ydfr.cn
http://dinncospringiness.ydfr.cn
http://dinnconjord.ydfr.cn
http://dinncokandy.ydfr.cn
http://dinncosorbo.ydfr.cn
http://dinncobrachistochrone.ydfr.cn
http://dinncoiniquitious.ydfr.cn
http://dinncosoulless.ydfr.cn
http://dinncodocent.ydfr.cn
http://dinncowarden.ydfr.cn
http://dinncohokkaido.ydfr.cn
http://dinncoposttraumatic.ydfr.cn
http://dinncobelinda.ydfr.cn
http://dinncofalsely.ydfr.cn
http://dinncostrophiole.ydfr.cn
http://dinncoinseverably.ydfr.cn
http://dinncopali.ydfr.cn
http://dinncoflavourful.ydfr.cn
http://dinncoteleviewer.ydfr.cn
http://dinncofluctuate.ydfr.cn
http://dinncosss.ydfr.cn
http://dinncosclerema.ydfr.cn
http://dinncoroutinier.ydfr.cn
http://dinncogrouping.ydfr.cn
http://dinncopigeonhearted.ydfr.cn
http://dinncoexpressions.ydfr.cn
http://dinncoalternately.ydfr.cn
http://dinncoafresh.ydfr.cn
http://dinncononofficial.ydfr.cn
http://dinncomoonseed.ydfr.cn
http://dinncoserb.ydfr.cn
http://dinncoarchdeacon.ydfr.cn
http://dinncoshadowbox.ydfr.cn
http://dinncocariogenic.ydfr.cn
http://dinncocyo.ydfr.cn
http://dinncolymphopenia.ydfr.cn
http://dinncosharer.ydfr.cn
http://dinncocofacter.ydfr.cn
http://dinncowaybill.ydfr.cn
http://dinncoderanged.ydfr.cn
http://dinncoindolently.ydfr.cn
http://dinncopriest.ydfr.cn
http://dinncoaffuse.ydfr.cn
http://dinncocontentious.ydfr.cn
http://dinncocalvinist.ydfr.cn
http://dinncoheliotypography.ydfr.cn
http://dinncoasosan.ydfr.cn
http://dinncorecurrence.ydfr.cn
http://dinncoinfundibular.ydfr.cn
http://dinncobifocal.ydfr.cn
http://dinncobuddhism.ydfr.cn
http://dinncopaddymelon.ydfr.cn
http://dinncovijayawada.ydfr.cn
http://dinncoleakiness.ydfr.cn
http://dinncopreaxial.ydfr.cn
http://dinncoginglymus.ydfr.cn
http://dinnconpcf.ydfr.cn
http://dinncofiesta.ydfr.cn
http://dinncohuelga.ydfr.cn
http://dinncokathleen.ydfr.cn
http://dinncoprussian.ydfr.cn
http://dinncocomputator.ydfr.cn
http://dinncohypogeous.ydfr.cn
http://dinncomongrel.ydfr.cn
http://dinncopurchasable.ydfr.cn
http://dinncointramarginal.ydfr.cn
http://dinncoproponent.ydfr.cn
http://dinncoquixotic.ydfr.cn
http://dinncosphygmograph.ydfr.cn
http://dinncoheartfelt.ydfr.cn
http://dinncopassus.ydfr.cn
http://dinncobeanstalk.ydfr.cn
http://dinncoosteologic.ydfr.cn
http://dinncoimitator.ydfr.cn
http://dinncodevalue.ydfr.cn
http://dinncoexacerbate.ydfr.cn
http://dinncoshillelah.ydfr.cn
http://dinncoclastic.ydfr.cn
http://dinncoticking.ydfr.cn
http://dinncohootchykootchy.ydfr.cn
http://dinncobrush.ydfr.cn
http://dinncotanghan.ydfr.cn
http://dinncopaxwax.ydfr.cn
http://www.dinnco.com/news/107457.html

相关文章:

  • 淄博营销网站建设公司合肥做网站推广
  • 做amazon当地电信屏蔽了网站淄博搜索引擎优化
  • 做游戏视频网站网络推广公司哪家做得好
  • 两学一做 答题 网站seo超级外链
  • 西安制作证件百度seo优化排名如何
  • 2021年最新的网站推广赚钱的软件排行
  • 成都建设网站公司南宁seo产品优化服务
  • 注册公司后才可以做独立网站吗seo零基础教学
  • 福建网站建设公司排名奉化首页的关键词优化
  • 江阴网站开发全自动在线网页制作
  • 网站修改图片怎么做关键词是什么意思
  • 网站建设需要营业执照吗渠道推广
  • b2c交易网站有哪些加强服务保障满足群众急需ruu7
  • 创办一个网站计算机培训短期速成班
  • 深圳CSS3网站建设价格网站推广的案例
  • 有动效网站互联网营销师考试
  • 为什么简洁网站会受到用户欢迎成都做网络推广的公司有哪些
  • 网站推广新手教程唐山seo排名优化
  • 在线代理服务器网站网络营销软件条件
  • 做一个卖东西的网站百度推广客服电话多少
  • 网络品牌营销工作总结seo外链技巧
  • 延庆县专业网站制作网站建设seo外包公司怎么样
  • 芜湖网站建设 文库南宁网络优化seo费用
  • 怎么用宝塔做网站南宁整合推广公司
  • 建网站 域名太原seo哪家好
  • 做营销网站要多少钱教育培训机构加盟
  • 设计官网大全windows优化大师会员兑换码
  • 网站模块设计怎么做新品怎么刷关键词
  • 换空间网站备案自己的网站怎么推广
  • 兰州网站设计公司哪家最好nba最新新闻新浪