1、CustomThreadPoolExecutor.java
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class CustomThreadPoolExecutor {
private ThreadPoolExecutor pool = null;
/**
* 线程池初始化方法
*
* corePoolSize 核心线程池大小----10
* maximumPoolSize 最大线程池大小----30
* keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间----30+单位TimeUnit
* TimeUnit keepAliveTime时间单位----TimeUnit.MINUTES
* workQueue 阻塞队列----new ArrayBlockingQueue<Runnable>(10)====10容量的阻塞队列
* threadFactory 新建线程工厂----new CustomThreadFactory()====定制的线程工厂
* rejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时,
* 即当提交第41个任务时(前面线程都没有执行完,此测试方法中用sleep(100)),
* 任务会交给RejectedExecutionHandler来处理
*/
public void init() {
pool = new ThreadPoolExecutor(
4,
8,
30,
TimeUnit.MINUTES,
// new ArrayBlockingQueue<Runnable>(10),
// new CustomThreadFactory(),
// new CustomRejectedExecutionHandler()
new LinkedBlockingDeque<Runnable>(4),
new ThreadPoolExecutor.CallerRunsPolicy()
);
}
public void destory() {
if(pool != null) {
pool.shutdownNow();
}
}
public ExecutorService getCustomThreadPoolExecutor() {
return this.pool;
}
private class CustomThreadFactory implements ThreadFactory {
private AtomicInteger count = new AtomicInteger(0);
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
String threadName = CustomThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);
System.out.println(threadName);
t.setName(threadName);
return t;
}
}
private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// 记录异常
// 报警处理等
System.out.println("error.............");
}
}
// 测试构造的线程池
public static void main(String[] args) {
CustomThreadPoolExecutor exec = new CustomThreadPoolExecutor();
// 1.初始化
exec.init();
ExecutorService pool = exec.getCustomThreadPoolExecutor();
for(int i=1; i<100; i++) {
System.out.println("提交第" + i + "个任务!");
pool.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("running=====");
}
});
}
// 2.销毁----此处不能销毁,因为任务没有提交执行完,如果销毁线程池,任务也就无法执行了
// exec.destory();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2、ServiceImpl
public static final int PAGESIZE = 40;
CustomThreadPoolExecutor exec = new CustomThreadPoolExecutor();
// 初始化线程池
exec.init();
ExecutorService pool = exec.getCustomThreadPoolExecutor();
int count = 0;
int total = entryList.size();
if (total < PAGESIZE) {
count = 1;
} else {
count = (total % PAGESIZE > 0) ? 1 + total / PAGESIZE : total / PAGESIZE;
}
List<DealPicInfoThread> tasks = Lists.newArrayList();
for (int i = 0; i < count; i++) {
DealPicInfoThread task = new DealPicInfoThread(i + 1, PAGESIZE, entryList,studentMapper);
tasks.add(task);
}
List<Future> future = pool.invokeAll((Collection) tasks);
for (int i = 0; i < future.size(); i++) {
future.get(i); //主线程等待所有线程的任务完成后才进行后面的操作
}
3、DealPicInfoThread.java
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.jeecg.common.util.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.zip.ZipEntry;
public class DealPicInfoThread implements Callable{
private static Logger logger = LoggerFactory.getLogger(DealPicInfoThread.class);
private int pageNum;
private int pageSize;
private List<ZipEntry> entryList;
private MwStudentMapper studentMapper;
public DealPicInfoThread(int pageNum, int pageSize, List<ZipEntry> entryList,MwStudentMapper studentMapper) {
this.pageNum = pageNum;
this.pageSize = pageSize;
this.studentMapper = studentMapper;
}
@Override
public Object call() {
int start = (pageNum-1)*pageSize;
int end = pageNum*pageSize;
for(int j = 0 ; j<entryList.size();j++){
if(j<start||j>=end){
continue;
}
dealInfo(entryList.get(j));
}
return null;
}
public void dealInfo(ZipEntry entry){
//多线程的业务代码
}
}