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