diff --git a/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java b/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java index a83919f67..68851e206 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java @@ -12,4 +12,9 @@ public class MiraiTaskManager { return MiraiTaskManager.instance; } + private MiraiTaskPool pool; + + private MiraiTaskManager(){ + + } } diff --git a/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskPool.java b/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskPool.java new file mode 100644 index 000000000..c865a6aed --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskPool.java @@ -0,0 +1,28 @@ +package net.mamoe.mirai.task; + +import java.util.concurrent.*; + +public class MiraiTaskPool { + + ExecutorService service; + + protected MiraiTaskPool(){ + this.service = Executors.newCachedThreadPool(); + } + + public Future submit(Callable callable, MiralTaskExceptionHandler handler) { + return this.service.submit(() -> { + try { + return callable.call(); + } catch (Throwable e) { + handler.onHandle(e); + return null; + } + }); + } + + public Future submit(Callable callable) { + return this.submit(callable, Throwable::printStackTrace); + } + +} diff --git a/mirai-core/src/main/java/net/mamoe/mirai/task/MiralTaskExceptionHandler.java b/mirai-core/src/main/java/net/mamoe/mirai/task/MiralTaskExceptionHandler.java new file mode 100644 index 000000000..ebe149c11 --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/task/MiralTaskExceptionHandler.java @@ -0,0 +1,6 @@ +package net.mamoe.mirai.task; + +@FunctionalInterface +public interface MiralTaskExceptionHandler { + void onHandle(Throwable e); +}