简单线程工具类
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolUtils {
volatile private static ThreadPoolExecutor threadPool = null;
public static void execute(Runnable runnable) {
getThreadPool().execute(runnable);
}
public static <T> Future<T> submit(Callable<T> callable) {
return getThreadPool().submit(callable);
}
private static ThreadPoolExecutor getThreadPool() {
try {
if (threadPool != null) {
} else {
synchronized (ThreadPoolUtils.class) {
if (threadPool == null) {
Thread.sleep(300);
int cpuNum = Runtime.getRuntime().availableProcessors();
int threadNum = cpuNum * 2 + 1;
threadPool = new ThreadPoolExecutor(
threadNum - 1,
threadNum,
Integer.MAX_VALUE,
TimeUnit.MILLISECONDS,
new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
super.rejectedExecution(r, e);
}
}
);
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return threadPool;
}
}