不太想看这种代码。太多新的语法糖了 看不明白
让ai写了个java的版本 我觉得好懂点
import java.util.concurrent.*;
import java.util.function.Supplier;
public class ThreadPool {
private final ExecutorService pool;
public ThreadPool(int threads) {
pool = Executors.newFixedThreadPool(threads);
}
public <T> CompletableFuture<T> enqueue(Callable<T> task) {
return CompletableFuture.supplyAsync(() -> {
try {
return task.call();
} catch (Exception e) {
throw new CompletionException(e);
}
}, pool);
}
public CompletableFuture<Void> enqueue(Runnable task) {
return CompletableFuture.runAsync(task, pool);
}
public void shutdown() { pool.shutdown(); }
}
--
FROM 210.75.217.*