我正在慢速连接上运行,我想在数据库中有新实体可用时设置一个带有回调的Speedment Stream。我该怎么做?
发布于 2017-05-06 07:51:14
这里有一种方法:
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)
);https://stackoverflow.com/questions/43815208
复制相似问题