Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin supplyAsync with executor

I want to create a CompletableFuture with a return value that runs on a specific executor in Kotlin.

The following code works just fine.

return CompletableFuture.supplyAsync {
        val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
        commandHandler.handle(command)
 }

However when I attempt to pass the executor it won't compile.

return CompletableFuture.supplyAsync({
        val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
        commandHandler.handle(command)
}, exec)

I tried to get clever and wrote the Java version and had Intellij covert it to Kotlin, but that one had the same error as well. What am I doing wrong here?

enter image description here

EDIT:

I can make it work by doing the following but it seems unnecessary. Can someone explain why this works but other methods do not. Are there other ways to write this code?

return CompletableFuture.supplyAsync(Supplier {
    commandHandler.handle(command)
}, exec) 
like image 965
jkratz55 Avatar asked Jan 22 '19 04:01

jkratz55


People also ask

How to create a completablefuture with a specific executor in Kotlin?

I want to create a CompletableFuture with a return value that runs on a specific executor in Kotlin. The following code works just fine. return CompletableFuture.supplyAsync { val commandHandler = registry.get<TCommand, TResponse> (command::class.java) commandHandler.handle (command) }

What is the use of supplyasync () with executor?

The task will be asynchronously completed running in given Executor and finally supplyAsync () will return new CompletableFuture with the value obtained by calling given Supplier. Find the sample code for supplyAsync () with Executor .

How to pass a supplier as a task to supplyasync () method?

We need to pass a Supplier as a task to supplyAsync () method. The task will be asynchronously completed running in given Executor and finally supplyAsync () will return new CompletableFuture with the value obtained by calling given Supplier. Find the sample code for supplyAsync () with Executor .

How do you handle asynchronous code in Kotlin?

Kotlin's approach to working with asynchronous code is using coroutines, which is the idea of suspendable computations, i.e. the idea that a function can suspend its execution at some point and resume later on.


1 Answers

I do not exactly know, why it is not working. But the following does work:

return CompletableFuture.supplyAsync({
    val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
    commandHandler.handle(command)
}, exec::execute)

As you can see, I changed the second parameter to a method reference. Now the signature of the method is:

supplyAsync(supplier: () -> U, executor: (Runnable)-> Unit)

If you pass the Executor directly, Kotlin chooses the signature:

supplyAsync(supplier: Supplier<U>, executor: Executor)

It looks like you can not mix interface and lambda style.

like image 152
Rene Avatar answered Oct 25 '22 11:10

Rene