Take the 2-minute tour ×

Standard scenario: User presses button and starts big task. EventThread creates SwingWorker to execute the task and get's on with life.

Now, since the big task is highly parallelizable, the first thing the SwingWorker thread does is to create a bunch of worker threads and farms the work out.

My question: Are the worker threads allowed to call SwingWorker#publish() to trigger a GUI update or is only the SwingWorker thread allowed to do so?

Thanks, Carsten

[Edit] Some pseudo code to make my use-case a bit clearer. The quesiton is basically: Is the code below OK? And what if I leave out waitForAllWorkerThreadsToFinish();?

public class MySwingWorker extends SwingWorker {

  class MyWorkerThread extends Runnable {
    void run() {
      while(!exitCondition) {

  public Void doInBackground() {

  void process(List<V> chunks) {
share|improve this question

2 Answers 2

up vote 2 down vote accepted

In general, no; publish() is intended to run on the background thread, which must itself synchronize all the results coming from the subsidiary workers. Fortunately, you have considerable latitude in designing how the background thread does this. For instance, this example uses CountDownLatch.

Addendum: waitForAllWorkerThreadsToFinish() looks like a good use case for CountDownLatch. MyWorkerThread definitely should not call publish() from a thread other than MySwingWorker without synchronizing access to any shared data. MyWorkerThread can update the GUI using invokeLater(), but the order won't be reliable.

Addendum: You asked the very practical question,

Do you have any reference for your answer, that [other] worker threads are not allowed to use publish()?

Memory Consistency Properties is a good summary. In effect, you would be calling publish() from multiple threads without synchronization. The results would be unpredictable.

share|improve this answer
Thanks for the answer. I think the CountDownLatch is not suitable in my case as the worker threads need to update the GUI often to display progress, not only when they are finished. Ideally they want to call publish() with some progress info that is then displayed in the GUI. –  Carsten Jun 29 '11 at 6:30
It might help to update your question with more details about the nature of the workers' interdependence. –  trashgod Jun 29 '11 at 6:37
Fair point. Pseudo Code example of my use case added. –  Carsten Jul 4 '11 at 6:43
I've elaborated above. Is there one overall progress indicator, or one for each thread? –  trashgod Jul 4 '11 at 7:43
One overall progress monitor. The actual set-up is different but you can think of it something like the worker threads publishing log messages that are displayed in a text view. They will iterate the while loop many times and should update the text view once per iteration. I'll use invokeLater(), but do you have any reference for your answer, that worker threads are not allowed to use publish()? –  Carsten Jul 5 '11 at 10:14

There isn't any limitation on which Threads can call SwingWorker#publish().

share|improve this answer
Do you have a reference or other information to back this up? –  Carsten Jul 7 '11 at 23:49
No, I just browsed the source code in Sun's JDK 1.6. The code does not put any limitations on the Threads that can call publish() and the main effect (adding chunks to the sun.swing.AccumulativeRunnable) is a synchronised operation. This is only applicable to Sun's JVM, but it would be peculiar to make this operation not Threadsafe in another JVM. –  Bringer128 Jul 8 '11 at 3:53

Your Answer


