I am writing an application that performs file menu actions using SwingWorker . Each method invoked returns a boolean value indicating whether the operation was successful or not.
I am currently using result expectation, for example:
public boolean executeOperation() { final SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() { @Override protected Boolean doInBackground() throws Exception { // .. if (aborted) { return false; } // .. return true; } }; worker.execute(); // busy wait while (!worker.isDone()) ; try { return worker.get().booleanValue(); } catch (Exception e) { // handle exceptions .. return false; } }
Is there a less intensive way of polling?
Using worker.get() will not work directly, since it blocks EDT, waiting for the task to complete - this means that even the dialogs that I open from SwingWorker will not be painted.
EDIT: if possible, I would like to avoid the method (or the worker) reporting their result asynchronously. I execute several short methods (file -> open, new, close, save, save as, exit) that rely on each other (i.e. when trying to exit, close calls close, close can cause save, save can cause save to form). Solving this asynchronously makes the code much more complex.
source share