gpt4 book ai didi

multithreading - 异步填充 Java Map 并将其作为 future 返回

转载 作者:行者123 更新时间:2023-12-03 12:43:48 27 4
gpt4 key购买 nike

我有一个创建成本很高的对象映射,因此我想创建对象并与我的应用程序中的其他进程并行填充映射。只有当主线程实际需要访问映射时,应用程序才应该等待填充映射的异步任务完成。我怎样才能最优雅地做到这一点?

目前的方法

目前,我可以使用 CompletableFuture.runAsync(Runnable, Executor) 在 map 本身中异步创建每个单独的对象。类似于下面的示例代码,但我不确定如何构建 Future/CompletableFuture用于返回 Map 的类型机制准备就绪时:

public static class AsynchronousMapPopulator {

private final Executor backgroundJobExecutor;

public AsynchronousMapPopulator(final Executor backgroundJobExecutor) {
this.backgroundJobExecutor = backgroundJobExecutor;
}

public ConcurrentMap<String, Integer> apply(final Map<String,Integer> input) {
final ConcurrentMap<String, Integer> result = new ConcurrentHashMap<>(input.size());
final Stream.Builder<CompletableFuture<Void>> incrementingJobs = Stream.builder();
for (final Entry<String, Integer> entry : input.entrySet()) {
final String className = entry.getKey();
final Integer oldValue = entry.getValue();
final CompletableFuture<Void> incrementingJob = CompletableFuture.runAsync(() -> {
result.put(className, oldValue + 1);
}, backgroundJobExecutor);
incrementingJobs.add(incrementingJob);
}
// TODO: This blocks until the training is done; Instead, return a
// future to the caller somehow
CompletableFuture.allOf(incrementingJobs.build().toArray(CompletableFuture[]::new)).join();
return result;
}
}

但是,使用上面的代码,当代码调用 AsynchronousTest.create(Map<String,Integer) ,它已经阻塞,直到该方法返回完全填充的 ConcurrentMap<String,Integer> ;我怎样才能把它变成类似 Future<Map<String,Integer>> 的东西以便我以后可以使用它?:
Executor someExecutor = ForkJoinPool.commonPool();
Future<Map<String,Integer>> futureClassModels = new AsynchronousMapPopulator(someExecutor).apply(wordClassObservations);
...
// Do lots of other stuff
...
Map<String,Integer> completedModels = futureClassModels.get();

最佳答案

正如@Holger 在他的评论中所说,您必须避免调用 .join()并依靠thenApply()相反,例如像这样:

public static class AsynchronousMapPopulator {

private final Executor backgroundJobExecutor;

public AsynchronousMapPopulator(final Executor backgroundJobExecutor) {
this.backgroundJobExecutor = backgroundJobExecutor;
}

public Future<Map<String, Integer>> apply(final Map<String,Integer> input) {
final ConcurrentMap<String, Integer> result = new ConcurrentHashMap<>(input.size());
final Stream.Builder<CompletableFuture<Void>> incrementingJobs = Stream.builder();
for (final Entry<String, Integer> entry : input.entrySet()) {
final String className = entry.getKey();
final Integer oldValue = entry.getValue();
final CompletableFuture<Void> incrementingJob = CompletableFuture.runAsync(() -> {
result.put(className, oldValue + 1);
}, backgroundJobExecutor);
incrementingJobs.add(incrementingJob);
}
// using thenApply instead of join here:
return CompletableFuture.allOf(
incrementingJobs.build().toArray(
CompletableFuture[]::new
)
).thenApply(x -> result);
}
}

关于multithreading - 异步填充 Java Map 并将其作为 future 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com