java 耗时任务_Java 使用线程池执行耗时方法

本文介绍了Java如何使用线程池管理耗时任务,通过ThreadPoolExecutor配置线程池参数,确保线程的合理调度。展示了如何创建固定大小的线程池,并提供了线程池的初始化、添加任务、关闭等操作。
摘要由CSDN通过智能技术生成

package group.hound.interfaces.infra.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.LinkedBlockingQueue;

import java.util.concurrent.ThreadFactory;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;/**

* 线程池管理(线程统一调度管理)

*

* @author jinliang 2020/02/19 19:43*/

public classThreadPoolManager {private static ThreadPoolManager sThreadPoolManager = newThreadPoolManager();/**

* 线程池基本大小*/

private static final int SIZE_CORE_POOL = 15;/**

* 线程池最大数量*/

private static final int SIZE_MAX_POOL = 15;/**

* 线程池单例创建方法

* @return sThreadPoolManager*/

public staticThreadPoolManager newInstance() {returnsThreadPoolManager;

}/**

* 线程工厂*/ThreadFactory namedThreadFactory= new ThreadFactoryBuilder().setNameFormat("cleanRecord-pool-%d").build();/**************************************************************************************************************

* 常见的几种线程技术

**************************************************************************************************************

* Java通过Executors提供四种线程池,分别为:

* newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

* newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

* newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。 newSingleThreadExecutor

* 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

*

* 1、public static ExecutorService newFixedThreadPool(int nThreads) {

* return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); }

*

* 2、 public static ExecutorService newSingleThreadExecutor() {

* return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue())); }

*

* 3、public static ExecutorService newCachedThreadPool() {

* return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue()); }

****************************************************************************************************************/

/**

* 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待

* @param corePoolSize - 池中所保存的线程数,包括空闲线程。

* @param maximumPoolSize - 池中允许的最大线程数。

* @param keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。

* @param unit - keepAliveTime 参数的时间单位。

* @param workQueue - 执行前用于保持任务的队列。此队列仅由保持 execute 方法提交的 Runnable 任务。

* @param handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。*/

private final ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL, 0L,

TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>(),namedThreadFactory, newThreadPoolExecutor.AbortPolicy());/**

* 将构造方法访问修饰符设为私有,禁止任意实例化。*/

privateThreadPoolManager() {

}/**

* 将线程池初始化,核心线程数量*/

public voidperpare() {if (mThreadPool.isShutdown() && !mThreadPool.prestartCoreThread()) {

@SuppressWarnings("unused")int startThread =mThreadPool.prestartAllCoreThreads();

}

}/**

* 向线程池中添加任务方法*/

public voidaddExecuteTask(Runnable task) {if (task != null) {

mThreadPool.execute(task);

}

}/**

* 判断是否是最后一个任务*/

protectedboolean isTaskEnd() {if (mThreadPool.getActiveCount() == 0) {return true;

}else{return false;

}

}/**

* 获取缓存大小*/

public intgetQueue(){returnmThreadPool.getQueue().size();

}/**

* 获取线程池中的线程数目*/

public intgetPoolSize(){returnmThreadPool.getPoolSize();

}/**

* 获取已完成的任务数*/

public longgetCompletedTaskCount(){returnmThreadPool.getCompletedTaskCount();

}/**

* 关闭线程池,不在接受新的任务,会把已接受的任务执行完*/

public voidshutdown() {

mThreadPool.shutdown();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值