Kotlin vs Java
public CompletableFuture<SomeOtherResult> loadAndCombine(String name1, String name2){
loadImageAsync(name1)
.thenAcceptBoth(loadImageAsync(name2)) ((one, two) -> {
combineImages(one, two)
)}
}
suspend fun loadAndCombine(name1: String, name2: String): Image =
coroutineScope {
val deferred1 = async { loadImage(name1) }
val deferred2 = async { loadImage(name2) }
combineImages(deferred1.await(), deferred2.await())
}
public <T> CompletableFuture<T> inTransaction(DatabaseFunction<T> fun) {
return CompletableFuture
.runAsync(() -> this.sendQuery("BEGIN"))
.thenApply(ignored -> fun.execute(this))
.thenApply(s -> {
this.sendQuery("COMMIT");
return s;
}).exceptionally(throwable -> {
this.sendQuery("ROLLBACK");
return null;
});
}
suspend fun <T> inTransaction(
f: suspend (Connection) -> T): T {
try {
this.sendQuery("BEGIN")
val result = f(this)
this.sendQuery("COMMIT")
return result
} catch (e: Throwable) {
this.sendQuery("ROLLBACK")
throw e
}
}
final var callables = Stream.of("Service A", "Service B", "Service C")
.map(DummyService::new)
.map(service -> (Callable<ContentDuration>) service::getContent)
.collect(Collectors.toList());
final var executor = Executors.newWorkStealingPool();
final var results = executor.invokeAll(callables).stream()
.map(
future -> {
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
).collect(Collectors.toList());
val DummyService.asyncContent: Deferred<ContentDuration>
get() = async(CommonPool) { content }
var results = runBlocking {
arrayOf("Service A", "Service B", "Service C")
.map { DummyService(it) }
.map { it.asyncContent }
.map { it.await() }
}