Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.concurrent.Future.get() not returning

I have the following Java code:

final Future future = exeService.submit(
    new Runnable() {
        public void run() {
            myObject.doSomething();
        }
    }
);

future.get();

where exeService is an instance of

java.util.concurrent.ExecutorService

The problem is that myObject.doSomething() never returns, and, hence, future.get() never returns.

However, if I replace the call to submit with a call to execute like this:

exeService.execute(
    new Runnable() {
        public void run() {
            myObject.doSomething();
        }
    }
);

the call to myObject.doSomething() does return. I don't know if it matters, but doSomething() is a void method.

Why is doSomething() finishing when using execute but not when using submit?

Also, I don't need to use Future.get(); that just seemed to be the most natural way of doing this. (I also run into the same problem with CountdownLatch.) The point is that I need to wait for doSomething() to finish before proceeding, and, for complicated reasons I won't go into here, I need to launch it on a separate thread. If there is another way of doing this that works, that would be fine.

like image 836
Paul Reiners Avatar asked Apr 06 '10 15:04

Paul Reiners


People also ask

How do you cancel a future in Java?

Canceling a Future With cancel () Suppose we triggered a task, but for some reason, we don't care about the result anymore. We can use Future.cancel (boolean) to tell the executor to stop the operation and interrupt its underlying thread: Our instance of Future, from the code above, will never complete its operation.

What is future in Java?

In this tutorial, we'll learn about Future. An interface that's been around since Java 1.5, it can be quite useful when working with asynchronous calls and concurrent processing. 2. Creating Futures Simply put, the Future class represents a future result of an asynchronous computation.

How do I execute a concurrency task?

This Java Concurrency tutorial guides you how to execute a task that computes a value and wait for the result available. This can be done by submitting a Callable task to an ExecutorService and getting the result via a Future object.

What is the future API in JavaScript?

In other words Future is a proxy or a wrapper around an object that is not yet there. Once the asynchronous computation is done, you can extract it. So what API does Future provide? Future.get () is the most important method. It blocks and waits until promised result is available ( resolved ).


1 Answers

As in Executor.execute() Javadoc:

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

So, the method execute() returns immediately leaving you with no option to query to status of submitted task.

On the other hand ExecutorService.submit():

Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.

The Future.get() will return only after successful competion, so never in your case.

This is further noted in Future.get() documentation:

Waits if necessary for the computation to complete, and then retrieves its result.

like image 139
pajton Avatar answered Sep 23 '22 01:09

pajton