Search code examples
javaconcurrencyrunnablefuturefuturetask

How to check when an executor has finished running a particular Runnable


I was looking for a way to wait and be notified when a particular Runnable has finished executing within an executor.

I found a FutureTask that has a method get, however, this returns a value. My program does not expect a return value; is there any data structure similar to FutureTask without needing to return a value? Almost like a FutureRunnable?

Thanks!


Solution

  • Maybe this method is what you need.

    The FutureTask is returned by the Executor when you submit a Runnable.

    Exemple:

    public class MyTask implements Runnable
    {
        @Override
        public void run()
        {
            ...
        }
    }
    
    ExecutorService s = Executors.newSingleThreadExecutor();
    Future<?> submit = s.submit(new Task());