문제

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!

도움이 되었습니까?

해결책 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());

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top