SwingWorker for both weekends and workers, or 1 for each?

Currently, I always create SwingWorkerto do my actual work, and another to handle output through PipedReader.

I would like to combine these two, but I just do not see a simple way to do this, in my situation.

Given that I:

  • You have a ready-made object that works ( converter, in this case)
  • Do you want to regularly show updates on converterprogress in the GUI
  • Get an object when it convertercompletes its task and returns.

Is there a way to do this without two SwingWorkers?

Edit: note that creating converterextend is SwingWorkernot an option.

Edit 2: in response to the comment below. This is part of a larger graphical application that processes large chunks of data (in the form of txt) and informs the user about what is wrong or what has been decided.

Code example (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());
   }
}
+3
source share
2 answers

If you use JDK7, you can use SecondaryLoop instead of combining multiple instances of SwingWorker.

SecondaryLoop essentially allows you to wait until the background thread (or series of threads) completes without blocking the EDT, like SwingWorker. It may not be suitable for replacing SwingWorker in every case, however, based on the description of your problem, it looks like this is something you could use.

, , : SecondaryLoop SwingWorker? "

+1

, SwingWorker, ...

@user1291492 30

+2

All Articles