Discussion:
How to build a swing dialog for aborting a running computation thread?
(too old to reply)
Herbert Meier
2006-09-15 16:00:02 UTC
Permalink
Hello,

I've the following problem: I want to start a computation in a seperate
thread, while this computation thread is runing the application should
show a dialog to the user with a button to abort the computation. Here
is a code sample:

-------------------------------------------------------------------

// ComputationThread is ab extension of java.lang.Thread which executes

// some long lasting computation in its run() method

ComputationThread thread = new ComputationThread();


// AbortDialog is a modal JDialog which shows a JButton("Abort")
// in actionPerformed() of the ActionListener at this Button
// the computation thread will be stopped: thread.interupt

AbortDialog abortDialog = new AbortDialog(this, thread);

// run computation
thread.start();

// show abortDialog
abortDialog.setVisible(true);


try {

// wait for computation thread to has finished
thread.join();

// dispose the abort dialog
abortDialog.dispose();

// ... do things with the computation results ...

} catch (InterruptedException e) { ... }

-------------------------------------------------------------------


The abort dialog should be modal, thus no other user input in the GUI
should be possile besides pressing the abort-button. But a modal dialog
seems to be waiting till it is closed until execution will proceed,
what means that thread.join won't be reached before closing the abot
dialog. But what i want is, that the abort dialog will be cloes after
thread.join automatically.

Many thanks if anybode could post a solution for this problem.


-herbert
d***@gmail.com
2006-09-15 16:20:03 UTC
Permalink
Hi Herbert,

There are certainly many ways you could go about this. The first that
comes to mind is to implement event code such that your dialog could be
a "ComputationListener" to your thread. The next which you might find
easier would be to do something like is shown here
(http://www.javaworld.com/javaworld/jw-06-2003/jw-0606-swingworker.html)
with the SwingWorker and when your computation is complete the finished
method closes the dialog if it is still open. Hope that helps you.

Cheers,

Dan Andrews

- - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - -
Post by Herbert Meier
Hello,
I've the following problem: I want to start a computation in a seperate
thread, while this computation thread is runing the application should
show a dialog to the user with a button to abort the computation. Here
-------------------------------------------------------------------
// ComputationThread is ab extension of java.lang.Thread which executes
// some long lasting computation in its run() method
ComputationThread thread = new ComputationThread();
// AbortDialog is a modal JDialog which shows a JButton("Abort")
// in actionPerformed() of the ActionListener at this Button
// the computation thread will be stopped: thread.interupt
AbortDialog abortDialog = new AbortDialog(this, thread);
// run computation
thread.start();
// show abortDialog
abortDialog.setVisible(true);
try {
// wait for computation thread to has finished
thread.join();
// dispose the abort dialog
abortDialog.dispose();
// ... do things with the computation results ...
} catch (InterruptedException e) { ... }
-------------------------------------------------------------------
The abort dialog should be modal, thus no other user input in the GUI
should be possile besides pressing the abort-button. But a modal dialog
seems to be waiting till it is closed until execution will proceed,
what means that thread.join won't be reached before closing the abot
dialog. But what i want is, that the abort dialog will be cloes after
thread.join automatically.
Many thanks if anybode could post a solution for this problem.
-herbert
Babu Kalakrishnan
2006-09-16 13:19:57 UTC
Permalink
Post by Herbert Meier
Hello,
I've the following problem: I want to start a computation in a seperate
thread, while this computation thread is runing the application should
show a dialog to the user with a button to abort the computation. Here
-------------------------------------------------------------------
// ComputationThread is ab extension of java.lang.Thread which executes
// some long lasting computation in its run() method
ComputationThread thread = new ComputationThread();
// AbortDialog is a modal JDialog which shows a JButton("Abort")
// in actionPerformed() of the ActionListener at this Button
// the computation thread will be stopped: thread.interupt
Make sure that you read the API documentation of the interrupt() method
and are aware of the fact that a thread will respond to the call
immediately only when it is blocked in a specified set of methods. If
the task does not perform any of these actions (for example - a task
that involves only numeric computations), you will not get an
InterruptedException or a ClosedByInterruptException - so you would have
to explicitly check for an interrupted status periodically.
Post by Herbert Meier
AbortDialog abortDialog = new AbortDialog(this, thread);
// run computation
thread.start();
// show abortDialog
abortDialog.setVisible(true);
If the dialog is modal (as you want), anything below this line will be
executed only after the Dialog is closed - that's the reason the
approach you've taken will not work, as you've observed.
Post by Herbert Meier
try {
// wait for computation thread to has finished
thread.join();
// dispose the abort dialog
abortDialog.dispose();
// ... do things with the computation results ...
} catch (InterruptedException e) { ... }
-------------------------------------------------------------------
The abort dialog should be modal, thus no other user input in the GUI
should be possile besides pressing the abort-button. But a modal dialog
seems to be waiting till it is closed until execution will proceed,
what means that thread.join won't be reached before closing the abot
dialog. But what i want is, that the abort dialog will be cloes after
thread.join automatically.
Many thanks if anybode could post a solution for this problem.
The closing of the dialog should be initiated by the task being
performed in the thread. For this you could use a custom event that is
fired by the task when it completes, and a listener could perform the
dialog closing function.

This can still lead to certain race conditions (at least theoretically).
For instance, if your thread completes very fast, there is a very small,
but finite chance that the call to dispose the dialog can occur before
the setVisible(true) call. This is a tricky problem to avoid, and I have
seen even production code that doesn't handle this condition. What I
have done to avoid this situation is to make sure that the
thread.start() call occurs only AFTER the dialog has been made visible.
This can be accomplished by registering a WindowListener on the Dialog,
and calling thread.start() from within the windowOpened event handler.

Also it might be better to use a SwingUtilities.invokeLater wrapper to
perform the dialog closing task since it is initiated from a non-EDT
thread. (I'm not cent percent sure if the dispose() method is
thread-safe). Also this might(?) alleviate the race condition because
tasks queued through invokeLater are only processed during the next
cycle of teh EDT - but I normally use both the WindowListenerr and the
invokeLater - just to be on the safer side.

BK

Loading...