ThreadPoolExecutor 线程池处理多任务 + 每个任务限定运行时长

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;

import java.util.concurrent.*;

public class PC4 {
    private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(4,
            6,
            10, TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(50));

    public static void main(String[] args) {
        executor.allowCoreThreadTimeOut(true);
        /*executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程名:" + Thread.currentThread().getName() + " ok");
                ThreadUtil.safeSleep(RandomUtil.randomInt(100, 1000));
            }
        });*/
        for (int i = 1; i <= 5; i++) {
            //假设五个任务
            int finalI = i;
            executor.execute(() -> {
                String threadName = Thread.currentThread().getName();
                ThreadUtil.safeSleep(RandomUtil.randomInt(100, 1000));
                for (int j = 1; j <= 10; j++) {
                    ThreadPoolExecutor executor_j = new ThreadPoolExecutor(1,
                            1,
                            10, TimeUnit.SECONDS,
                            new LinkedBlockingDeque<>(1));
                    executor_j.allowCoreThreadTimeOut(true);
                    int finalJ = j;
                    Future<String> future = executor_j.submit(() -> {
                        String threadName_j = Thread.currentThread().getName();
                        String msg = "i线程名:" + threadName + ";j线程名:" + threadName_j + " ok->i=" + finalI + ";j=" + finalJ;
                        System.out.println(msg);
                        return msg;
                    });
                    try {
                        //限制时长4分钟
                        future.get(4, TimeUnit.MINUTES);
                    } catch (InterruptedException | ExecutionException | TimeoutException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}

效果图

i线程名:pool-1-thread-4;j线程名:pool-2-thread-1 ok->i=4;j=1
i线程名:pool-1-thread-4;j线程名:pool-3-thread-1 ok->i=4;j=2
i线程名:pool-1-thread-4;j线程名:pool-4-thread-1 ok->i=4;j=3
i线程名:pool-1-thread-4;j线程名:pool-5-thread-1 ok->i=4;j=4
i线程名:pool-1-thread-4;j线程名:pool-6-thread-1 ok->i=4;j=5
i线程名:pool-1-thread-4;j线程名:pool-7-thread-1 ok->i=4;j=6
i线程名:pool-1-thread-4;j线程名:pool-8-thread-1 ok->i=4;j=7
i线程名:pool-1-thread-4;j线程名:pool-9-thread-1 ok->i=4;j=8
i线程名:pool-1-thread-4;j线程名:pool-10-thread-1 ok->i=4;j=9
i线程名:pool-1-thread-4;j线程名:pool-11-thread-1 ok->i=4;j=10
i线程名:pool-1-thread-3;j线程名:pool-12-thread-1 ok->i=3;j=1
i线程名:pool-1-thread-3;j线程名:pool-13-thread-1 ok->i=3;j=2
i线程名:pool-1-thread-3;j线程名:pool-14-thread-1 ok->i=3;j=3
i线程名:pool-1-thread-3;j线程名:pool-15-thread-1 ok->i=3;j=4
i线程名:pool-1-thread-3;j线程名:pool-16-thread-1 ok->i=3;j=5
i线程名:pool-1-thread-3;j线程名:pool-17-thread-1 ok->i=3;j=6
i线程名:pool-1-thread-3;j线程名:pool-18-thread-1 ok->i=3;j=7
i线程名:pool-1-thread-3;j线程名:pool-19-thread-1 ok->i=3;j=8
i线程名:pool-1-thread-3;j线程名:pool-20-thread-1 ok->i=3;j=9
i线程名:pool-1-thread-3;j线程名:pool-21-thread-1 ok->i=3;j=10
i线程名:pool-1-thread-1;j线程名:pool-22-thread-1 ok->i=1;j=1
i线程名:pool-1-thread-1;j线程名:pool-23-thread-1 ok->i=1;j=2
i线程名:pool-1-thread-1;j线程名:pool-24-thread-1 ok->i=1;j=3
i线程名:pool-1-thread-1;j线程名:pool-25-thread-1 ok->i=1;j=4
i线程名:pool-1-thread-1;j线程名:pool-26-thread-1 ok->i=1;j=5
i线程名:pool-1-thread-1;j线程名:pool-27-thread-1 ok->i=1;j=6
i线程名:pool-1-thread-1;j线程名:pool-28-thread-1 ok->i=1;j=7
i线程名:pool-1-thread-1;j线程名:pool-29-thread-1 ok->i=1;j=8
i线程名:pool-1-thread-1;j线程名:pool-30-thread-1 ok->i=1;j=9
i线程名:pool-1-thread-1;j线程名:pool-31-thread-1 ok->i=1;j=10
i线程名:pool-1-thread-2;j线程名:pool-32-thread-1 ok->i=2;j=1
i线程名:pool-1-thread-2;j线程名:pool-33-thread-1 ok->i=2;j=2
i线程名:pool-1-thread-2;j线程名:pool-34-thread-1 ok->i=2;j=3
i线程名:pool-1-thread-2;j线程名:pool-35-thread-1 ok->i=2;j=4
i线程名:pool-1-thread-2;j线程名:pool-36-thread-1 ok->i=2;j=5
i线程名:pool-1-thread-2;j线程名:pool-37-thread-1 ok->i=2;j=6
i线程名:pool-1-thread-2;j线程名:pool-38-thread-1 ok->i=2;j=7
i线程名:pool-1-thread-2;j线程名:pool-39-thread-1 ok->i=2;j=8
i线程名:pool-1-thread-2;j线程名:pool-40-thread-1 ok->i=2;j=9
i线程名:pool-1-thread-2;j线程名:pool-41-thread-1 ok->i=2;j=10
i线程名:pool-1-thread-4;j线程名:pool-42-thread-1 ok->i=5;j=1
i线程名:pool-1-thread-4;j线程名:pool-43-thread-1 ok->i=5;j=2
i线程名:pool-1-thread-4;j线程名:pool-44-thread-1 ok->i=5;j=3
i线程名:pool-1-thread-4;j线程名:pool-45-thread-1 ok->i=5;j=4
i线程名:pool-1-thread-4;j线程名:pool-46-thread-1 ok->i=5;j=5
i线程名:pool-1-thread-4;j线程名:pool-47-thread-1 ok->i=5;j=6
i线程名:pool-1-thread-4;j线程名:pool-48-thread-1 ok->i=5;j=7
i线程名:pool-1-thread-4;j线程名:pool-49-thread-1 ok->i=5;j=8
i线程名:pool-1-thread-4;j线程名:pool-50-thread-1 ok->i=5;j=9
i线程名:pool-1-thread-4;j线程名:pool-51-thread-1 ok->i=5;j=10

ThreadPoolExecutorJava中的一个线程池实现类。它继承自ExecutorService接口,可以用来管理和执行线程任务ThreadPoolExecutor线程池提供了更灵活的线程管理和任务调度的功能,并且可以根据需要进行配置。可以通过指定核心线程数、最大线程数、线程存活时间和任务队列等参数来创建和配置ThreadPoolExecutor线程池。 使用ThreadPoolExecutor线程池可以提供以下几个优点: 1. 降低线程创建和销毁的开销。线程池可以重用已经创建的线程,减少了频繁创建和销毁线程的开销。 2. 提高系统的响应速度。线程池可以并发执行多个任务,提高了系统的处理能力和响应速度。 3. 控制线程并发数量。通过设置线程池的核心线程数和最大线程数,可以控制系统的并发线程数量,避免资源耗尽和系统崩溃的风险。 4. 提供任务调度和管理。线程池可以将任务按照一定的策略和优先级进行调度和执行,方便管理任务的执行顺序和优先级。 总之,ThreadPoolExecutor线程池是一个灵活可配置的线程管理和任务调度工具,可以提高系统的并发处理能力和响应速度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [线程池ThreadPoolExecutor详解(整理详细)](https://blog.csdn.net/trusause/article/details/125747447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [ThreadPoolExecutor线程池的使用方法](https://download.csdn.net/download/weixin_38659648/12746355)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值