JDK线程池和Spring线程池实例,异步调用,可以直接使用
(1)JDK线程池的使用,此处采用单例的方式提供,见示例:
public class ThreadPoolUtil {
private static int corePoolSize = 5;
private static int maximumPoolSize = 10;
private static long keepAliveTime = 60L;
private static TimeUnit unit = TimeUnit.SECONDS;
private static int capacity = 1024;
private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("jdk-thread-pool-%d").build();
private static final ExecutorService executorService = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
new LinkedBlockingQueue<>(capacity),
namedThreadFactory,
new ThreadPoolExecutor.AbortPolicy()); private ThreadPoolUtil () { } public static ExecutorService getExecutorService () {
return executorService;
}
} 在其它地方可以直接这样使用:
ThreadPoolUtil.getExecutorService().execute(() -> {
System.out.println("test1");
System.out.println("test2");
}) (2)Spring线程池的使用,此处通过配置类的方式配置线程池的相关属性,见示例:
@Configuration
@EnableAsync
public class DocataThreadBeanConfig {private int corePoolSize = 5;private int maxPoolSize = 10;private int queueCapacity = 1024;private String namePrefix = "async-service-task-";
// 上述属性可以通过@Value来读取配置值 @Bean(name = "asyncServiceTaskExecutor")
public TaskExecutor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(corePoolSize);
// 设置最大线程数
executor.setMaxPoolSize(maxPoolSize);
// 设置队列容量
executor.setQueueCapacity(queueCapacity);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
// 设置默认线程名称
executor.setThreadNamePrefix(namePrefix);
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();;
return executor;
}
} 在其它文件中需要这样使用:
@Resource(name="asyncServiceTaskExecutor")
private ThreadPoolTaskExecutor asyncServiceTaskExecutor; 不要直接使用@Autowired,否则会提示失败的
@Autowired
private ThreadPoolTaskExecutor asyncServiceTaskExecutor;

------20191128闪

JDK线程池和Spring线程池的使用的更多相关文章

  1. spring线程池(同步、异步)

    一.spring异步线程池类图 二.简单介绍 2.1. TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent.Executor 以下是官方已经 ...

  2. Spring线程池配置模板设计(基于Springboot)

    目录 线程池配置模板 基础的注解解释 常用配置参数 配置类设计 线程池使用 ThreadPoolTaskExecutor源码 线程池配置模板 springboot给我们提供了一个线程池的实现,它的底层 ...

  3. spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue

    一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...

  4. java和spring 线程池总结

    1. spring 的线程池 ThreadPoolTaskExecutor @Configuration public class ThreadPoolConfig { @Bean("thr ...

  5. 分享知识-快乐自己:Spring线程池配置

    Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. Spring 配 ...

  6. spring线程池的同步和异步(1)

    spring线程池(同步.异步) 一.spring异步线程池类图 二.简单介绍 2.1. TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent ...

  7. 007-多线程-JUC线程池-Spring线程池配置、池子如何配置参数

    一.概述 Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. 1.1 ...

  8. spring线程池配置

    源自:http://zjriso.iteye.com/blog/771706 1.了解 TaskExecutor接口 Spring的TaskExecutor接口等同于java.util.concurr ...

  9. Spring线程池开发实战

    Spring线程池开发实战 作者:chszs,转载需注明. 作者博客主页:http://blog.csdn.net/chszs 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然, ...

随机推荐

  1. File类和枚举

    java.io.File类:文件和目录路径名的抽象表示形式 File类常见构造方法: File(String pathname):通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例. ...

  2. Hexo | 超详细的hexo+githhub page搭建过程

    首先安装node.js 安装git 去Git官网根据你的电脑参数,下载对应版本. 下载完成,通过在命令行输入 git version 查看是否安装成功,有输出版本号说明安装成功. 鼠标邮件菜单里就多了 ...

  3. java架构之路-(dubbo专题)dubbo的基本使用

    今天我们来说一下dubbo的基本使用,没有什么高阶的知识,真的超级简单易懂. Dubbo核心功能解释 dubbo 阿里开源的一个SOA服务治理框架,从目前来看把它称作是一个RPC远程调用框架更为贴切. ...

  4. codewars--js--Convert all the cases!

    问题描述: In this kata, you will make a function that converts between camelCase, snake_case, and kebab- ...

  5. JavaScript-其他设计模式

    其他设计模式 JavaScript 中不常用 对应不到经典场景 原型模式-行为型 clone 自己,生成一个新对象 java 默认有 clone 接口,不用自己实现 //'object.creat'用 ...

  6. openlayers6结合geoserver实现地图矢量瓦片(附源码下载)

    内容概览 1.基于openlayers6结合geoserver实现地图矢量瓦片2.源代码demo下载 效果图如下: 实现思路:利用Geoserver发布矢量切片服务,然后openlayers调用矢量瓦 ...

  7. Escape(反思与总结)

    题目描述: BH is in a maze,the maze is a matrix,he wants to escape! Input: The input consists of multiple ...

  8. 安装vue-devools

    https://blog.csdn.net/weixin_38654336/article/details/80790698

  9. vue-cli-service 报错

    错误内容: vue-cli-service serve /bin/sh: vue-cli-service: command not found error Command failed with ex ...

  10. django-分页(非海量数据)

    views.py class AnalysisDataHandler(View): def get(self, request): analysis_data = MonitorCenterDataA ...