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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值