orpqK
orpqK

Reputation: 2785

When is a Future task finished

Is a Future task considered finished, isDone()==true when it hits the return statement? For example:

ExecutorService executor = newSingleThreadExecutor();
Future<> result = executor.submit(new Runnable()
            {
                @Override
                public void run()
                {
                   return;
                }
            }

Is result considered done when it hits the return ?

Upvotes: 4

Views: 1999

Answers (2)

Ammar
Ammar

Reputation: 4024

Completion of Future.get is considered to pin point a Future's completion.

To quote from java docs

A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task...

In other words till the time Future doesn't complete the call to Future.get will be blocked and hence completion of Future.get should be considered as completion of Future (normal or exceptional)

P.S.: Future.isDone should only be used to check the status and no way an indication as when the Future was completed.

Upvotes: 2

John Vint
John Vint

Reputation: 40256

More or less. The javadoc is ambiguous for what it exactly means but generally, when the callable returns, very shortly after isDone() will be true (or if an exception occurs the same is true).

Upvotes: 3

Related Questions