Skip to content

Commit

Permalink
调整 RunUtil 执行器分离为 parallelExecutor + asyncExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
noear committed Sep 20, 2023
1 parent 203ddd2 commit 6922584
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions UPDATE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* 添加 多配置文件交差引用变量支持
* 添加 DownloadedFile(file,name) 构造函数
* 添加 Router 对 405 的支持
* 调整 RunUtil 执行器分离为 parallelExecutor + asyncExecutor
* 调整 CacheService 接口(增加类型化 get)
* 调整 SessionState 接口(增加类型化 get)
* 调整 Context::session 接口(增加类型化 get)
Expand Down
53 changes: 43 additions & 10 deletions solon/src/main/java/org/noear/solon/core/util/RunUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,31 @@
* @since 1.12
*/
public class RunUtil {
private static ExecutorService executor;
/**
* 并行执行器(一般用于执行简单的定时任务)
*/
private static ExecutorService parallelExecutor;
/**
* 异步执行器(一般用于执行 @Async 注解任务)
*/
private static ExecutorService asyncExecutor;
/**
* 调度执行器(一般用于延时任务)
*/
private static ScheduledExecutorService scheduledExecutor;

static {
executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
parallelExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
new NamedThreadFactory("Solon-executor-"));
new NamedThreadFactory("Solon-parallelExecutor-"));

int asyncPoolSize = Runtime.getRuntime().availableProcessors() * 2;
asyncExecutor = new ThreadPoolExecutor(asyncPoolSize, asyncPoolSize,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
new NamedThreadFactory("Solon-asyncExecutor-"));

scheduledExecutor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
new NamedThreadFactory("Solon-echeduledExecutor-"));
}
Expand All @@ -32,10 +49,26 @@ public static void setScheduledExecutor(ScheduledExecutorService scheduledExecut
}
}

/**
* @deprecated 2.5
*/
@Deprecated
public static void setExecutor(ExecutorService executor) {
if (executor != null) {
ExecutorService old = RunUtil.executor;
RunUtil.executor = executor;
setParallelExecutor(executor);
}

public static void setParallelExecutor(ExecutorService parallelExecutor) {
if (parallelExecutor != null) {
ExecutorService old = RunUtil.parallelExecutor;
RunUtil.parallelExecutor = parallelExecutor;
old.shutdown();
}
}

public static void setAsyncExecutor(ExecutorService asyncExecutor) {
if (asyncExecutor != null) {
ExecutorService old = RunUtil.asyncExecutor;
RunUtil.asyncExecutor = asyncExecutor;
old.shutdown();
}
}
Expand All @@ -60,28 +93,28 @@ public static void runOrThrow(RunnableEx task) {
* 并行执行
*/
public static Future<?> parallel(Runnable task) {
return executor.submit(task);
return parallelExecutor.submit(task);
}

/**
* 并行执行
*/
public static <T> Future<T> parallel(Callable<T> task) {
return executor.submit(task);
return parallelExecutor.submit(task);
}

/**
* 异步执行
*/
public static CompletableFuture<Void> async(Runnable task) {
return CompletableFuture.runAsync(task, executor);
return CompletableFuture.runAsync(task, asyncExecutor);
}

/**
* 异步执行
*/
public static <U> CompletableFuture<U> async(Supplier<U> task) {
return CompletableFuture.supplyAsync(task, executor);
return CompletableFuture.supplyAsync(task, asyncExecutor);
}

/**
Expand Down

0 comments on commit 6922584

Please sign in to comment.