思路:
第一步:创建代理类
第二步:创建方法
第三步:执行类中方法
可封装为Manager
package com.future.googleplay.manager;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolManager {
public ThreadPoolProxy proxt=new ThreadPoolProxy(3, 5, 500);
private static ThreadPoolManager manager;
private ThreadPoolManager(){}
public static ThreadPoolManager getInstance(){
if(manager==null){
manager=new ThreadPoolManager();
}
return manager;
}
public class ThreadPoolProxy {
// 代理类
private int corePoolSize;
private int keepAliveTime;
private int maximumPoolSize;
private ThreadPoolExecutor threadPoolExecutor;
public ThreadPoolProxy(int corePoolSize, int maximumPoolSize,
int keepAliveTime) {
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.keepAliveTime = keepAliveTime;
}
// 执行任务的方法
public void excete(Runnable runnable){
if(threadPoolExecutor==null){
threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, //核心线程数
maximumPoolSize, //最大线程数
keepAliveTime, //线程空闲的存活时间
TimeUnit.MILLISECONDS, //存活时间的单位 /毫秒
new LinkedBlockingQueue(),//队列
Executors.defaultThreadFactory());//线程产生的工厂
}
//线程池执行任务
threadPoolExecutor.execute(runnable);
}
}
}