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缓存线程池的更多相关文章

  1. (CSDN 迁移) JAVA多线程实现-可回收缓存线程池(newCachedThreadPool)

    在前两篇博客中介绍了单线程化线程池(newSingleThreadExecutor).可控最大并发数线程池(newFixedThreadPool).下面介绍的是第三种newCachedThreadPo ...

  2. 8、java5线程池之动态缓存线程池newCachedThreadPool

    JDK文档描述 创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们.对于执行很多短期异步任务的程序而言,这些线程池通常可提高程序性能.调用 execute 将重用以前构造的线程 ...

  3. 阿里开源支持缓存线程池的ThreadLocal Transmittable ThreadLocal(TTL)

    功能 在使用线程池等会缓存线程的组件情况下,提供ThreadLocal值的传递功能. JDK的InheritableThreadLocal类可以完成父子线程值的传递. 但对于使用线程池等会缓存线程的组 ...

  4. 常见线程池之 newCacheThreadPool 缓存线程池 简单使用

    package com.aaa.threaddemo; import java.util.concurrent.BlockingQueue; import java.util.concurrent.E ...

  5. JDK线程池的使用

    转载自:https://my.oschina.net/hosee/blog/614319: 摘要: 本系列基于炼数成金课程,为了更好的学习,做了系列的记录. 本文主要介绍: 1. 线程池的基本使用 2 ...

  6. JDK提供的四种线程池代码详解

    一.线程池什么时候使用,会给我们带来什么好处? 如果很多用户去访问服务器,用户访问服务器的时间是非常短暂的,那么有可能在创建线程和销毁线程上花费的时间会远远大于访问所消耗的时间,如果采用线程池会使线程 ...

  7. Executor框架(三)线程池详细介绍与ThreadPoolExecutor

    本文将介绍线程池的设计细节,这些细节与 ThreadPoolExecutor类的参数一一对应,所以,将直接通过此类介绍线程池. ThreadPoolExecutor类 简介   java.uitl.c ...

  8. JAVA线程池的创建与使用

    为什么要用线程池? 我们都知道,每一次创建一个线程,JVM后面的工作包括:为线程建立虚拟机栈.本地方法栈.程序计数器的内存空间(下图可看出),所以线程过多容易导致内存空间溢出.同时,当频繁的创建和销毁 ...

  9. Java线程池工作原理

    前言 当项目中有频繁创建线程的场景时,往往会用到线程池来提高效率.所以,线程池在项目开发过程中的出场率是很高的. 那线程池是怎么工作的呢?它什么时候创建线程对象,如何保证线程安全... 什么时候创建线 ...

随机推荐

  1. Spring Cloud Hystrix原理篇(十一)

    一.Hystrix处理流程 Hystrix流程图如下: Hystrix整个工作流如下: 构造一个 HystrixCommand或HystrixObservableCommand对象,用于封装请求,并在 ...

  2. Atcoder abc187 F Close Group(动态规划)

    Atcoder abc187 F Close Group 题目 给出一张n个点,m条边的无向图,问删除任意数量的边后,留下来的最少数量的团的个数(\(n \le 18\) ) 题解 核心:枚举状态+动 ...

  3. mysql远程访问被拒绝问题

    远程连接MySql数据库时: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 远 ...

  4. 自定义 简单 底部tab

    项目地址:https://gitee.com/jielov/music-netease-api.git 先创建三个页面 分别为 home.vue , classify.vue, my.vue . 以下 ...

  5. 短信平台软件开发,短信发送平台销售,短信软件源码,G客短信发送平台

    一:web短信平台组成  需要短信软件平台源码的联系QQ:290615413 vx:290615413  整套短信系统平台还是由B/S(客户端+后台,取消了以前C/S的管理后台) ,C/S发送服务端和 ...

  6. 风炫安全WEB安全学习第二十六节课 XSS常见绕过防御技巧

    风炫安全WEB安全学习第二十六节课 XSS常见绕过防御技巧 XSS绕过-过滤-编码 核心思想 后台过滤了特殊字符,比如说

  7. 腾讯消息队列CMQ部署与验证

    环境 IP 备注 192.168.1.66 node1 前置机 192.168.1.110 node2 192.168.1.202 node3 架构图 组件介绍 组件 监听端口 access 1200 ...

  8. Spring Boot 2.0 的配置绑定类Bindable居然如此强大

    1. 前言 在开发Spring Boot应用时会用到根据条件来向Spring IoC容器注入Bean.比如配置文件存在了某个配置属性才注入Bean : 图中红色的部分是说,只有ali.pay.v1.a ...

  9. 如何在 Vite 中使用 Element UI + Vue 3

    在上篇文章<2021新年 Vue3.0 + Element UI 尝鲜小记>里,我们尝试使用了 Vue CLI 创建 Vue 3 + Element UI 的项目,而 Vue CLI 实际 ...

  10. QA职责