How to signal caller thread that ExecutorService completed a task

From the main thread:

executorService.submit(new Runnable() { ... }); 

Now that Runnable has completed execution, is there a standard Java method that signals the thread of the calling thread that it completed without creating a new interface / listener class?

Bonus points if the signal can be removed from the caller’s stream.

+4
source share
3 answers

submit returns a Future on which the feed can call get to lock until the task completes.

 Future<?> future = executor.submit(new Runnable() { ... }); future.get(); 
+4
source

You can lock the get () object of the returned Future object or query the isDone () method. Alternatively, you can use the goava goava library, which is ListenableFuture .

+3
source

I am not sure about your implementation and how many threads will be running at the same time, so this may not work. One thing that you might pay attention to is to force your own signal to start, that it is completed by calling a method that knows how to update the user interface component that is needed.

0
source

Source: https://habr.com/ru/post/1413856/


All Articles