#Returning Futures

1 messages · Page 1 of 1 (latest)

vale meteor
#

Hello, I am using Futures to return promises of results of a thread. But is it possible to 'fork' the execution of the function to execute a snippet of code just after finishing the thread? Here's the idea of the code.

public Future invoke_async(Map<Object, Object> params) {
  Funtion <Map<Object,Object>, Object> action = foo.getAction();
  Callable<Object> callable = () -> action.apply(params);
  ExecutorService executorService = Executors.newSingleThreadExecutor();
  Future future = executorService.submit(callable);
  //Here I would run a snippet of code after finishing the thread
  return future;
}```
pseudo muskBOT
# vale meteor Hello, I am using Futures to return promises of results of a thread. But is it p...

Detected code, here are some useful tools:

Formatted code
public Future invoke_async(Map<Object, Object> params) {
  Funtion<Map<Object, Object>, Object> action = foo.getAction();
  Callable<Object> callable = () -> action.apply(params);
  ExecutorService executorService = Executors.newSingleThreadExecutor();
  Future future = executorService.submit(callable);
  //Here I would run a snippet of code after finishing the thread
  return future;
}
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

execution of the thread, without blocking the main thread?

slow latch
#

one is to use CompletableFuture and its .then, which just chains some code

#

the other is to .join the futures and just run something afterwards

#

3rd secret way is to use a StructuredTaskScope

#

but that relies on a preview feature

vale meteor
#

Thank you for the help I will look it up peepo_happy peepo_heart