java常用线程池

时间:2022-04-16 18:05:16
package com.pk.multithread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
*
* 各种线程池
* @author linxianbin
*
*/
public class ThreadPool {

//固定大小的线程池
private static ExecutorService fixedThredPool=null;
//单任务的线程池
private static ExecutorService singlePool=null;

//创建可变尺寸的线程池
private staticExecutorService cachedPool=null;

//延迟执行任务的线程池
private static ScheduledExecutorService scheduledTpool=null;


/**
* 延迟执行任务的线程池
* @return
*/
public static ScheduledExecutorService getScheduledTpool() {

if (scheduledTpool==null) {
scheduledTpool=Executors.newScheduledThreadPool(10);
}
return scheduledTpool;
}

/**
* 创建可变尺寸的线程池
* @return
*/
public static ExecutorService getCachedPool() {

if (cachedPool==null) {
cachedPool=Executors.newCachedThreadPool();
}
return cachedPool;
}
/**
* 单任务的线程池
* @return
*/
public static ExecutorService getSinglePool() {

if (singlePool==null) {
singlePool=Executors.newSingleThreadExecutor();
}
return singlePool;
}
/**
* 固定大小的线程池
* @return
*/
public static ExecutorService getFixedThreadPool() {
if (fixedThredPool==null) {
fixedThredPool=Executors.newFixedThreadPool(10);
}
return fixedThredPool;
}

}