Pergunta

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!

Foi útil?

Solução 2

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());

Outras dicas

There is no mechanism (not even theoretically) which would somehow "notify the parent thread" when the task is done. First, the thread from which you submit a task can't post festum become the parent thread of the ExecutorService's internal thread; and second, if your calling thread isn't specifically dedicated to listening for such events, it can't be "jacked into" from outside, interrupting whatever it is doing, and receive the notification.

With standard Java's ExecutorService the API you sadly don't get asynchronous notifications via callbacks; all you get is polling methods and blocking methods.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top