1、创建线程池
public class AsyncThreadPoolUtil {
private AsyncThreadPoolUtil(){}
private static final String THEAD_NAME_PRE_FILE_MANAGER = "file-manege-";
private static final AtomicInteger FILE_MANAGER_THREAD_NO = new AtomicInteger(0);
private static final ExecutorService FILE_MANAGE_TREAD_POOL;
static {
FILE_MANAGE_TREAD_POOL = new ThreadPoolExecutor(4,6,60,TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
// 自定义线程名称
r -> new Thread(r,
THEAD_NAME_PRE_FILE_MANAGER + FILE_MANAGER_THREAD_NO.getAndIncrement()
)
);
}
public static ExecutorService getFileManageTreadPool() {
return FILE_MANAGE_TREAD_POOL;
}
}
- corePoolSize:4
- 线程池中保留的核心线程数量,即使这些线程处于空闲状态,它们也不会被销毁,除非设置了允许核心线程超时
- maximumPoolSize: 6
- 指定线程池允许创建的最大线程数量。
- 当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务。
- 当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常
- LinkedBlockingQueue: 任务队列,默认队列长度Integer.MAX_VALUE
2、创建带有返回值的Callable
@Slf4j
public class MultipleDownloadTask<T> implements Callable<T> {
private String filePath;
MultipleDownloadTask(String filePath) {
this.filePath = filePath;
}
@Override
public T call() throws Exception {
log.info("task--start--{}", filePath);
// 具体下载任务
try {
Thread.sleep(1000 * 10);
}catch (Exception e) {
e.printStackTrace();
}
log.info("task--end--{}", filePath);
return (T) filePath;
}
}
3、创建下载Utils
@Slf4j
public class PoolDownloadUtils {
public static <T> List<T> multipleDownloadHttp(List<String> paths) {
if (ObjectUtils.isEmpty(paths)) {
throw new RuntimeException("multiple--下载路径为空");
}
// 线程池
final ExecutorService fileManageTreadPool = AsyncThreadPoolUtil.getFileManageTreadPool();
// 保存线程执行结果
List<Future<T>> futureList = new ArrayList<>();
for (String path: paths) {
// 创建下载任务
MultipleDownloadTask<T> task = new MultipleDownloadTask<>(path);
// 往线程池中提交任务 如果线程池设置不合理会直接报错,但是线程中的任务还是会继续执行
Future<T> future = fileManageTreadPool.submit(task);
futureList.add(future);
}
// 执行等待返回
return disponseMultipleDownloadResponse(futureList);
}
private static <T> List<T> disponseMultipleDownloadResponse(List<Future<T>> futureList) {
// 将线程执行结果组装返回
List<T> responseList = new ArrayList<>();
StopWatch stopWatch = new StopWatch();
stopWatch.start("mulDown--all");
for (Future<T> future : futureList) {
StopWatch stopWatchIn = new StopWatch();
stopWatchIn.start("mulDown--in");
try {
// future.get等待线程的执行结果,如果核心线程池是4个,这是4个4个一起过来
T stream = future.get(11, TimeUnit.SECONDS);
responseList.add(stream);
} catch (Exception e) {
log.info("mulDown--future--{}", e.getMessage());
future.cancel(true);
}
stopWatchIn.stop();
log.info("mulDown--in--{}", stopWatchIn.getLastTaskTimeMillis());
}
stopWatch.stop();
log.info("mulDown--all--{}", stopWatch.getLastTaskTimeMillis());
return responseList;
}
}
4、main执行
public static void main(String[] args) {
List<String> addList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
addList.add("任务-" + i);
}
List<String> streamList = PoolDownloadUtils.multipleDownloadHttp(addList);
log.info("back--{}", streamList);
}
5、模拟执行结果
Connected to the target VM, address: '127.0.0.1:56064', transport: 'socket'
23:27:11.799 [file-manege-2] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-2
23:27:11.799 [file-manege-1] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-1
23:27:11.799 [file-manege-0] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-0
23:27:11.799 [file-manege-3] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-3
23:27:11.804 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--all--等待结果返回
-- 以上四个【任务】直接打印因为
1-核心线程4个只会有四个线程【file-manege-xx】执行
2-多余的任务会放入队列
3-当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务。
4-当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常
23:27:21.814 [file-manege-2] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-2
23:27:21.816 [file-manege-2] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-4
23:27:21.816 [file-manege-1] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-1
23:27:21.816 [file-manege-3] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-3
23:27:21.816 [file-manege-0] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-0
23:27:21.816 [file-manege-1] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-5
23:27:21.816 [file-manege-3] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-6
23:27:21.816 [file-manege-0] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-7
23:27:21.817 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--10002
23:27:21.818 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--0
23:27:21.818 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--0
23:27:21.818 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--0
-- 以上任务继续执行因为
1-T stream = future.get(20, TimeUnit.SECONDS);会阻塞当前线程,20为等待任务执行的超时时间
2-每个线程模拟了10秒,也就是说第一组四个线程,10秒后才会通过get
3-4个线程其中一个执行完任务,则会继续从队列中获取一个任务,继续执行
23:27:31.816 [file-manege-2] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-4
23:27:31.817 [file-manege-0] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-7
23:27:31.817 [file-manege-3] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-6
23:27:31.817 [file-manege-0] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-9
23:27:31.817 [file-manege-1] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-5
23:27:31.817 [file-manege-2] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--start--任务-8
23:27:31.818 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--9999
23:27:31.818 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--0
23:27:31.818 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--0
23:27:31.818 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--0
-- 以上步骤都是线程有空闲了,从队列获取任务,继续执行
23:27:41.823 [file-manege-2] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-8
23:27:41.823 [file-manege-0] INFO com.ljw.morethreadline.pool.MultipleDownloadTask - task--end--任务-9
23:27:41.823 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--10004
23:27:41.823 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--in--0
-- 以下是所有任务执行完毕,结束
23:27:41.824 [main] INFO com.ljw.morethreadline.pool.PoolDownloadUtils - mulDown--all--30019
23:27:41.824 [main] INFO com.ljw.morethreadline.service.PoolService - back--[任务-0, 任务-1, 任务-2, 任务-3, 任务-4, 任务-5, 任务-6, 任务-7, 任务-8, 任务-9]