
com.star.thread.GlobalThreadPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
some utility class for java develop
The newest version!
package com.star.thread;
import com.star.exception.ToolException;
import com.star.string.StringUtil;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 全局线程池
*/
public final class GlobalThreadPool {
/**
* 执行器
*/
private static ExecutorService executor;
/**
* 锁
*/
private static Object lock = new Object();
static {
init();
}
private GlobalThreadPool() {
}
/**
* 初始化池
*/
public static void init() {
synchronized (lock) {
if (executor != null) {
executor.shutdown();
}
executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
new SynchronousQueue<>(), new NamedThreadFactory(StringUtil.EMPTY, false));
}
}
/**
* 关闭池
*
* @param isNow 现在关闭
*/
public static void shutdown(final boolean isNow) {
synchronized (lock) {
if (executor != null) {
if (isNow) {
executor.shutdownNow();
} else {
executor.shutdown();
}
}
}
}
/**
* 执行线程
*
* @param runnable 线程
*/
public static void execute(final Runnable runnable) {
try {
executor.execute(runnable);
} catch (Exception e) {
throw new ToolException(StringUtil.format("executing runnable failure: {}", e.getMessage()), e);
}
}
/**
* 执行线程(有返回值)
*
* @param callable 线程
* @param 范型
* @return future
*/
public static Future execute(final Callable callable) {
try {
return executor.submit(callable);
} catch (Exception e) {
throw new ToolException(StringUtil.format("executing callable failure: {}", e.getMessage()), e);
}
}
/**
* 执行线程
*
* @param runnable 线程
* @return future
*/
public static Future> submit(final Runnable runnable) {
return executor.submit(runnable);
}
/**
* 获得ExecutorService
*
* @return ExecutorService
*/
public static ExecutorService getExecutor() {
return executor;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy