Long running tasks and swing progress bars


I recently dusted off some code I had had written a while back to serve as a mini-framework (Swing based) for long background tasks that updated progress bars as they executed. The code is compact and easy to to use, but lacked a facility to allow the user to cancel the task. I updated the code to provide this facility today, and wanted to share it for review, comment and possible re-use.


The primary design criterion that drove the design of this mini-framework is ease of use. I feel that the end result is indeed easy to use, requiring, in most cases, a one line invocation and an anonymous implementation of an interface (see below).


To set the stage for this discussion, let us review some common characteristics of long running background tasks.


As the name suggests, long running background tasks are "long running": they are slow enough to seriously impact "responsiveness" in applications providing user interactivity. These tasks usually involve some heavy duty local or remote processing, and never involve direct user interaction. Examples of such tasks are searching all files under a given sub directory for occurences of a textual token (which is what "grep -R" does), bulk loading data into a database, or performing an intensive mathematical calculation.


Often, long running background tasks involve a number of iterations performing a simpler task, and the iteration count is often known in advance. For example, the number of files to search, or the number of records to insert, or the number of times a square root must be calculated are often known at the outset of the task. Sometimes, however, an iteration count is not known in advance, perhaps due to the nature of the problem, or perhaps due to the nature of the algorithm (which might be recursive, not iterative).


Having described the type of problem we are trying to solve, here is a quick look at an application that performs a background task (counting up from 0 to 100 in a "for" loop, updating a progress bar as it works):



The calling thread is blocked while the "exec" method runs. A progress bar is shown to the user to indicate progress. The long running task calls "progress.increment()" regularly to update this progress bar. Another thing that the long running task does is check the "interrupted" status of its thread: if the interrupted status indicates that the thread has been interrupted, the long running task interprets this to mean that the user has cancelled the task, and it returns immediately.


In this example, we have a SecureRandom object named "random", and we use it repeatedly to generate a random "sleep time" during each iteration. This is done to simulate the "long running"-ness of this task. Running this in a "main" program gives:



All that the user must do is implement the "ISwingRunnableWithProgress" interface, which has just one method:



The classes used to implement the mini-framework are provided in the supplied source code (see below). The TaskRunner class is the lynchpin of this min-framework. Its "exec" method (see the code sample above) launches the background task and positions the progress bar dialog in the center of the screen.



The TaskRunner class implements the ICancellable interface:



The TaskRunner class adds the current instance as a listener for cancel events to the progress dialog in its constructor:



and it interrupts the background thread running the long running task if its "cancelled" method is invoked (by the progress dialog, if the user presses the Cancel button):



To try out this mini-framework, download gui-progress.jar to a temporary directory and run "java -jar gui-progress.jar". The source code for the mini-framework is available in gui-progress-src.zip. This file contains source code for a number of other, unrelated classes as well, which you are free to ignore.


 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.