Currently I always create a SwingWorker to do my actual work, and another to process the output via a PipedReader.
I'd love to combine the two, but I just don't see an easy way to do it, in my situation.
Given that I:
converter, in this case)converter's progress to the GUIconverter finishes its task and returns.Is there a way to do this without two SwingWorkers?
Edit: Note that making converter extend SwingWorker is not an option.
Edit 2: In response to comment below. This is part of a larger GUI application, processing large chunks of data (in the form of txt) and informing the user what is wrong, or what has been decided.
Example Code (purely illustrative, not used):
//SwingWorker 1
@Override
protected Void doInBackground() {
outputWorker.useInStream(new PipedReader(outStream));
outputWorker.execute();
converter.usePipedWriter(outStream);
Output object2=converter.Convert(object1);
}
//SwingWorker 2
@Override
protected Void doInBackground() {
while(inStream.hasNext()) {
publish(inStream.next());
}
}
not an answer to your question if use one or two SwingWorker, I want to complicating these things ...
I'd suggest to look at Executor that invoke SwingWorker and the same code with real visualisations / output to the GUI
Executor doesn't care somhow about SwingWorkers lifecycle and vice versa
@user1291492 Processing 30 mb text files
you have to listening for exception(s) from methods get() , expecially read answer by @trashgod
you have to (Processing 30 mb text files) to implementing some Batch, for example every 10loops you publish data to the GUI, then output to the GUI will be immediatelly
during Batch is possible to stop the current thread by SwingWorker#cancel();
If you are using JDK7, you might consider using SecondaryLoop instead of chaining together multiple SwingWorker instances.
SecondaryLoop essentially allows you to wait for a background thread (or series of threads) to complete without blocking the EDT, similar to SwingWorker. It is may not be suitable as a replacement for SwingWorker in every case, however based on the description of your problem it sounds like this is something you might be able to use.
For more discussion on this topic, please take a look related question, "SecondaryLoop instead of SwingWorker? "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With