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();
}
}

最新文章

  1. 在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录
  2. ABP源码分析四十三:ZERO的本地化
  3. Usual tiny skills &amp; solutions
  4. [tem]线段树(白书版)
  5. call 和 apply使用
  6. 通过源码成功启动odoo 10.0
  7. IOS 蓝牙相关-app作为外设被连接的实现(3)
  8. register_chrdev_region/alloc_chrdev_region和cdev注册字符设备驱动
  9. Objective-C开发图书推荐
  10. poj 1695 动态规划
  11. 认识变量------JAVA
  12. height为100%的问题
  13. python construct文档
  14. knockout 与checkbox联动
  15. hdu5336 Walk Out
  16. 【Luogu3121】审查(AC自动机)
  17. 剑指Offer——分治算法
  18. WPF:通过名字找控件
  19. Linux 三剑客 -- awk sed grep
  20. 动态规划法(六)鸡蛋掉落问题(一)(egg dropping problem)

热门文章

  1. Node项目模板管理脚手架ptm-cli开发
  2. 基础篇:JAVA原子组件和同步组件
  3. 风炫安全WEB安全学习第二十五节课 利用XSS键盘记录
  4. TensorFlow中数据读取—如何载入样本
  5. netty服务端客户端启动流程分析
  6. Docker学习笔记之搭建Docker私有仓库
  7. 如何实现CentOS服务器的扩容??
  8. 【Linux】使用grep快速比较两个文件不同
  9. oracle常见进度查询脚本
  10. PW2320芯片N沟道增强型MOSFET