Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ability to get the progress on a Future<T> object

With reference to the java.util.concurrent package and the Future interface I notice (unless I am mistaken) that the ability to start a lengthy tasks and be able to query on the progress only comes with the SwingWorker implementing class.

This begs the following question:

Is there a way, in a non-GUI, non-Swing application (imaging a console application) to start a lengthy task in the background and allow the other threads to inspect the progress ? It seems to me that there is no reason why this capability should be limited to swing / GUI applications. Otherwise, the only available option, the way I see it, is to go through ExecutorService::submit which returns a Future object. However, the base Future interface does not allow monitoring the progress.

like image 310
Marcus Junius Brutus Avatar asked Feb 16 '10 19:02

Marcus Junius Brutus


People also ask

How can we get result of Future object?

Line 14: We get a future object as a result of submitting the callable to the executor service. Lines 16 to 19: We wait for the future to complete in a while loop. Line 21: We get the result of the future using the get() method. Line 23: The result is printed to the console.

How to get Future result in Java?

Get Result From FutureIf you call the get() method before the asynchronous task has completed, the get() method will block until the result is ready. There is a version of the get() method which can time out after an amount of time has passed which you can specify via method parameters.

How to create a Future object in Java?

The easiest way to create an ExecutorService is to use one of the factory methods of the Executors class. After the creation of the asynchronous task, a Java Future object is returned from the executor. If you'd like to read more about The Executor Framework, we've got an in-depth article on that.

What is the use of Future object?

The future object can be used to check the status of a Callable and then retrieve the result from the Callable once the thread is done. It also provides timeout functionality.


1 Answers

Obviously, the Future object would only be good for blocking and then receiving the result.

The Runnable or Callable object that you submit would either have to know how to provide this progress (percentage complete, count of attempts, status (enum?) etc) and provide that as an API call to the object itself, or posted in some lookup resource (in memory map or database if necessary). For simplicity I tend to like the object itself, especially since you're going to most likely need a handle (id) to lookup the object or a reference to the object itself.

This does mean that you have 3 threads operating. 1 for the actual work, 1 that is blocked while waiting for the result, and 1 that is a monitoring thread. The last one could be shared depending on your requirements.

like image 199
Matt Avatar answered Oct 14 '22 23:10

Matt