This commit is contained in:
liujiahua123123 2019-08-09 00:05:18 +08:00
parent 9a877b2b11
commit 9e3d1209cf
3 changed files with 39 additions and 0 deletions

View File

@ -12,4 +12,9 @@ public class MiraiTaskManager {
return MiraiTaskManager.instance;
}
private MiraiTaskPool pool;
private MiraiTaskManager(){
}
}

View File

@ -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 <D> Future<D> submit(Callable<D> callable, MiralTaskExceptionHandler handler) {
return this.service.submit(() -> {
try {
return callable.call();
} catch (Throwable e) {
handler.onHandle(e);
return null;
}
});
}
public <D> Future<D> submit(Callable<D> callable) {
return this.submit(callable, Throwable::printStackTrace);
}
}

View File

@ -0,0 +1,6 @@
package net.mamoe.mirai.task;
@FunctionalInterface
public interface MiralTaskExceptionHandler {
void onHandle(Throwable e);
}