views:

84

answers:

2

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.

+1  A: 

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.

Matt
A: 

Thanks. I was hoping that there was a standard concurrency framework way to stay updated on the progress of a long running task without requiring the client program to worry about orchestrating and synchronizing everything correctly. It seemed to me to that one could fathom an extended version of the Future interface that would support: public short progress(); in addition to the usual isDone() and get() methods. Obviously the implementation of the progress() would then need to poll the object directly so maybe Future would need to be specified as Future where CanReportProgress is the following interface: public interface CanReportProgress { public short progress(); } This begs the question of why one would bother to go through the Future object as opposed to calling the object itself to get the progress. I don't know. I'll have to give it more thought. It could be argued that it is closer to the current contract / semantics whereby the Callable object is not, itself, accessed again by the client programmer after the call to ExecutorService::submit / execute.

Menelaos
I don't know of anything out of the JDK box that would help you much other than to take the SwingWorker (part of 1.6 JDK) as an example of how a non-Swing version might look. It uses RunnableFuture<T> and a getProgress() (with event notification)). This sounds like a solid start to me, since it's a known quantity and has been under development/test for years by greater minds. Best of luck.
Matt