gpt4 book ai didi

java - 使用 Speedment Streams 进行异步回调

转载 作者:行者123 更新时间:2023-12-02 13:15:15 25 4
gpt4 key购买 nike

我正在缓慢的连接上运行,并且我想在数据库中有新实体可用时设置带有回调的 Speedment Stream。我该怎么做?

最佳答案

这是一种实现方法:

public class Main {

public static void main(String[] args) {

DemoApplication app = new DemoApplicationBuilder()
.withPassword("mySecretPw")
.build();

final Manager<Country> countries =
app.getOrThrow(CountryManager.class);

ForkJoinPool.commonPool().submit(
() -> countries.stream().forEach(Main::callback)
);

}

private static void callback(Country c) {
System.out.format("Thread %s consumed %s%n",
Thread.currentThread().getName(), c);
}

}

当然,您可以向 ForkJoinPool 提供任何流。例如,如果您只想在回调中接收名称以“A”开头的前十个国家/地区,那么您可以编写:

ForkJoinPool.commonPool().submit(
() -> countries.stream()
.filter(Country.NAME.startsWith("A"))
.limit(10)
.forEach(Main::callback)
);

关于java - 使用 Speedment Streams 进行异步回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43815208/

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