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

公司网站制作公司排名青岛有没有做网站的

公司网站制作公司排名,青岛有没有做网站的,哪家建设网站好,免费php源码网【线程与线程池】线程数设置 0. 全局使用一个线程池业务中使用优雅关闭线程池#xff08;如在应用退出时#xff09;另一种方法 1. 按照任务类型对线程池进行分类1.1 Netty的IO处理任务#xff0c;就是典型的IO密集型任务 2. 混合型任务创建线程池时#xff0c;如何确定线程… 【线程与线程池】线程数设置 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 对象 FutureString 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.w-s-a.com/news/439863/

相关文章:

  • 中山 五金 骏域网站建设专家专门用来制作网页的软件是什么
  • 怎么做刷东西的网站数据分析软件工具有哪些
  • 官方购物网站正品交易网站域名
  • lol网站建设seo 网站太小
  • 网站建设销售职责手机网站制作软件
  • 福州百度企业网站seo如何在电脑上登录wordpress
  • 开发区全力做好网站建设网络广告营销成功案例
  • 114网站建设高并发系统架构
  • php网站打开一片空白wordpress中文广告插件下载
  • 怎样建自己的网站免费的百度关键词排名点击
  • 医院网站建设的特点怎么查看网站百度快照
  • 网站 如何备案一般网站开发公司
  • 做网站的公司 贵阳郑州新像素ui设计培训收费
  • 温州网站建设公司电话给个免费的网址
  • 个人做电子商务网站备案软考高级
  • 淘宝客需要自己做网站吗四川遂宁做网站的公司
  • 编写网站策划书缘魁上海网站建设
  • 梧州外贸网站推广设计wordpress 上传 七牛
  • 增加网站备案千灯做网站
  • 深圳做网站的公php做简易网站
  • 徐州哪家做网站好商业空间设计效果图
  • 重庆建网站cqiezscom大学毕业做网站插画师好吗
  • 在门户网站做产品seo怎么样做网站管理员
  • 动画做视频在线观看网站字体安装+wordpress
  • vs2015网站开发做珠宝建个网站推广怎么样
  • 大桥外语官方网站星做宝贝佛山微信网站开发
  • 河南建设网站公司哪家好怎样做一家网站
  • 安阳市哪里做网站建设网站流量怎么赚钱
  • 网站开发与优化课程总结软件班级网站建设
  • py网站开发wordpress 公司网站 模板 下载