1、依据系统合理创建线程池
/** * 获取系统处理器个数,作为线程池数量 */ int nThreads = Runtime.getRuntime().availableProcessors(); ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("pool-%d").build(); /** * Common Thread Pool */ ExecutorService pool = new ThreadPoolExecutor(nThreads, 200, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
2、任务的创建
package com.hikvision.highwaydoc.util; import com.hikvision.highwaydoc.constants.EnumMinioType; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.concurrent.Callable; /** * @author liuyueyi * @version 1.0.0 * @ClassName UploadPicThread.java * @Description ---- * @createTime 2023年01月04日 19:42:00 */ public class UploadPicTask implements Callable<String> { private MinioTool minioTool; private byte[] data; private String fileName; private EnumMinioType minioBucket; public UploadPicTask(MinioTool minioTool, byte[] data, String fileName, EnumMinioType minioBucket) { this.minioTool = minioTool; this.data = data; this.fileName = fileName; this.minioBucket = minioBucket; } @Override public String call() throws Exception {
// 业务代码 InputStream inputStream; inputStream = new ByteArrayInputStream(data); String url = minioTool.uploadFile(inputStream, fileName, minioBucket); return url; } }
3、线程池的使用(多任务以及单任务一样处理)
ArrayList<Future<String>> uploadPicTaskList = new ArrayList<>(); for (PictureDataDTO pictureDataDTO : pictureDataDTOSaveList) { Future<String> future = pool.submit(new UploadPicTask(minioTool, pictureDataDTO.getPictureData().getData(), pictureDataDTO.getDocPicId() + suffix, EnumMinioType.OTHER)); uploadPicTaskList.add(future);
}
标签:minioBucket,高效,minioTool,--,fileName,线程,new,data From: https://www.cnblogs.com/l-926/p/17039722.html