package com.cnic.utils;
import lombok.SneakyThrows;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolUtil {
private static ThreadPoolExecutor threadPoolExecutor = null;
private ThreadPoolUtil() {
}
public static ThreadPoolExecutor getThreadPoolExecutor() {
if (threadPoolExecutor == null) {
synchronized (ThreadPoolUtil.class) {
if (threadPoolExecutor == null) {
threadPoolExecutor = new ThreadPoolExecutor(4,
20,
60,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>());
}
}
}
return threadPoolExecutor;
}
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
for (int i = 0; i < 10; i++) {
threadPoolExecutor.execute(new Runnable() {
@SneakyThrows
@Override
public void run() {
System.out.println("************" + Thread.currentThread().getName() + "************");
Thread.sleep(2000);
}
});
}
}
}
ThreadPoolUtil
最新推荐文章于 2024-01-04 12:04:00 发布