mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-31 11:30:16 +08:00
task
This commit is contained in:
parent
9a877b2b11
commit
9e3d1209cf
@ -12,4 +12,9 @@ public class MiraiTaskManager {
|
||||
return MiraiTaskManager.instance;
|
||||
}
|
||||
|
||||
private MiraiTaskPool pool;
|
||||
|
||||
private MiraiTaskManager(){
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package net.mamoe.mirai.task;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MiralTaskExceptionHandler {
|
||||
void onHandle(Throwable e);
|
||||
}
|
Loading…
Reference in New Issue
Block a user