cachedThreadPool缓存线程池
package com.loan.modules.common.util; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; @SuppressWarnings("all")
public class ETLThreadPool { private static ThreadPoolExecutor etlExectutor = null; /**
* 功能:得到线程池实例
* @param corePoolSize 线程池维护线程的最少数量
* @param maximumPoolSize 线程池维护线程的最大数量
* @param keepAliveTime 线程池维护线程所允许的空闲时间
* @param unit 线程池维护线程所允许的空闲时间的单位
* @param workQueue 线程池所使用的缓冲队列
* @return
*/
@SuppressWarnings("unchecked")
public static ThreadPoolExecutor getThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) {
synchronized (ETLThreadPool.class) {
if (etlExectutor == null) { etlExectutor = createExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
workQueue);
}
} return etlExectutor;
} /**
* 功能:创建ThreadPoolExecutor实例;
* @param corePoolSize:核心线程数量
* @param maximumPoolSize:最大线程数量
* @param keepAliveTime:线程空闲保持时间
* @param unit:时间单位
* @param workQueue:工作队列
* @param handler:旧任务抛弃策略
* @return
* ThreadPoolExecutor
*/
@SuppressWarnings("unchecked")
private static ThreadPoolExecutor createExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue) {
etlExectutor =
new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
workQueue);
return etlExectutor;
} }
package com.loan.modules; import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import com.loan.modules.common.util.ETLThreadPool; public class test {
private static ThreadPoolExecutor cachedThreadPool = ETLThreadPool.getThreadPool(8,10, 3000, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(20000));
public static void main(String[] args) throws InterruptedException {
Thred1();
Thred2();
Thred1();
}
public static void Thred1() throws InterruptedException{
int total = 30;
final CountDownLatch countDownLatch = new CountDownLatch(total);
for (int i = 0; i < total; i++) {
Thred1 t1 = new Thred1(i,countDownLatch);
cachedThreadPool.execute(t1);
}
countDownLatch.await();// 等待所有子线程执行完 } public static void Thred2() throws InterruptedException{
int total = 5;
final CountDownLatch countDownLatch = new CountDownLatch(total);
for (int i = 0; i < total; i++) {
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
// 批量向instinct系统发送进件信息
// 计数器 减一
System.out.println("2");
countDownLatch.countDown();
}
});
}
countDownLatch.await();// 等待所有子线程执行完
} private synchronized static int getQueueSize(Queue queue)
{
return queue.size();
}
}
cachedThreadPool缓存线程池的更多相关文章
- (CSDN 迁移) JAVA多线程实现-可回收缓存线程池(newCachedThreadPool)
在前两篇博客中介绍了单线程化线程池(newSingleThreadExecutor).可控最大并发数线程池(newFixedThreadPool).下面介绍的是第三种newCachedThreadPo ...
- 8、java5线程池之动态缓存线程池newCachedThreadPool
JDK文档描述 创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们.对于执行很多短期异步任务的程序而言,这些线程池通常可提高程序性能.调用 execute 将重用以前构造的线程 ...
- 阿里开源支持缓存线程池的ThreadLocal Transmittable ThreadLocal(TTL)
功能 在使用线程池等会缓存线程的组件情况下,提供ThreadLocal值的传递功能. JDK的InheritableThreadLocal类可以完成父子线程值的传递. 但对于使用线程池等会缓存线程的组 ...
- 常见线程池之 newCacheThreadPool 缓存线程池 简单使用
package com.aaa.threaddemo; import java.util.concurrent.BlockingQueue; import java.util.concurrent.E ...
- JDK线程池的使用
转载自:https://my.oschina.net/hosee/blog/614319: 摘要: 本系列基于炼数成金课程,为了更好的学习,做了系列的记录. 本文主要介绍: 1. 线程池的基本使用 2 ...
- JDK提供的四种线程池代码详解
一.线程池什么时候使用,会给我们带来什么好处? 如果很多用户去访问服务器,用户访问服务器的时间是非常短暂的,那么有可能在创建线程和销毁线程上花费的时间会远远大于访问所消耗的时间,如果采用线程池会使线程 ...
- Executor框架(三)线程池详细介绍与ThreadPoolExecutor
本文将介绍线程池的设计细节,这些细节与 ThreadPoolExecutor类的参数一一对应,所以,将直接通过此类介绍线程池. ThreadPoolExecutor类 简介 java.uitl.c ...
- JAVA线程池的创建与使用
为什么要用线程池? 我们都知道,每一次创建一个线程,JVM后面的工作包括:为线程建立虚拟机栈.本地方法栈.程序计数器的内存空间(下图可看出),所以线程过多容易导致内存空间溢出.同时,当频繁的创建和销毁 ...
- Java线程池工作原理
前言 当项目中有频繁创建线程的场景时,往往会用到线程池来提高效率.所以,线程池在项目开发过程中的出场率是很高的. 那线程池是怎么工作的呢?它什么时候创建线程对象,如何保证线程安全... 什么时候创建线 ...
随机推荐
- centos6.8环境搭建
1.centos6 yum源失效解决方案 下载使用阿里的云仓库 curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Cent ...
- linux kernel操作GPIO函数
一.头文件 #include <asm/gpio.h> 二.注册 GPIO int gpio_request(unsigned gpio, const char *label) 功能:申请 ...
- Linux SSH , SCP 建立信任关系(免密传输)
最近有个需求,Jenkins需要将war传输到各个项目节点中,所以需要远程执行各个节点的shell脚本.但是中间有个输入密码的过程,在自动化部署中是行不通的,故需要增加免密登录.具体如下: 如果想在 ...
- 阿里面试官:什么是MySQL索引,为什么要有索引?
一.什么是索引? 索引就好比字典的目录一样 我们通常都会先去目录查找关键偏旁或者字母再去查找 要比直接翻查字典查询要快很多 二.为什么要有索引? 然而我们在使用mysql数据库的时候也像字典一样有索引 ...
- Head First 设计模式 —— 12. 状态 (State) 模式
思考题 public class GumballMachine { final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; fi ...
- Azure Key Valut 简介
Azure Key Vault(密钥库)是用于安全地存储和访问Secret的云服务,Secret是需要严格控制访问权限的内容,例如API密钥,密码,证书或加密密钥.Key Vault Service支 ...
- Restful API是什么、为什么、怎么使用
Restful API 文章目录 Restful API 1.REST是什么以及它的 6 个限制 REST是什么? REST的6个限制 2. Restful是什么 Restful是什么 RESTful ...
- 分别使用 Python 和 Math.Net 调用优化算法
1. Rosenbrock 函数 在数学最优化中,Rosenbrock 函数是一个用来测试最优化算法性能的非凸函数,由Howard Harry Rosenbrock 在 1960 年提出 .也称为 R ...
- Nginx(六):配置解析之location解析
nginx成为非常流行的代理服务软件,最根本的原因也许是在于其强悍性能.但还有一些必要的条件,比如功能的完整,配置的易用,能够解决各种各样的实际需求问题,这些是一个好的软件的必备特性. 那么,今天我们 ...
- SSRF - Pikachu
概述: SSRF(Server-Side Request Forgery:服务器端请求伪造) 其形成的原因大都是由于服务端提供了从其他服务器应用获取数据的功能,但又没有对目标地址做严格过滤与限制 导致 ...