title: SwingWorker.process() not getting called in a command line application
body: <p>I have observed a strange behaviour of a command line application that invokes SwingWorkers. The code is non-optimal in the sense that it creates a lot of thread pools. However, due to the control of the <code>generation</code> variable, all these pools except the last one do not execute any code. It implies that threads from these pools not only do not participate in races for the locks, but should also be garbage collected and vanished.</p>
  
  <p>A minimal working example (not making anything useful) is the following:</p>
  
  <pre><code>package test;
  
  import java.util.List;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import javax.swing.SwingWorker;
  
  public class Tester {
  
  private final int threads;
  private ExecutorService threadPool;
  private final int size;
  private long currGen;
  private int left;
  private int done;
  
  public Tester(int size, int threads) {
      this.threads = threads;
  
      this.size = size;
  
      this.currGen = 0;
  
      this.left = size;
      this.done = 0;
  }
  
  private class TestWorker extends SwingWorker&lt;Object, Object&gt; {
      private final Tester tester;
      private final long generation;
  
      TestWorker(Tester tester, long generation) {
          this.tester = tester;
          this.generation = generation;
      }
  
      @Override
      protected Object doInBackground() throws Exception {
          while(this.tester.get(generation)) {
              Thread.sleep(1);
              publish(1);
          }
  
          return null;
      }
  
      @Override
      protected void process(List&lt;Object&gt; results) {
          for(Object n : results) {
              this.tester.put(generation);
          }
      }
  }
  
  public void run() {      
      this.currGen++;        
      this.left = size;
      this.done = 0;
  
      System.out.printf("Starting %d\n", currGen);
  
      this.threadPool = Executors.newFixedThreadPool(threads + 4);
  
      for (int threadId = 0; threadId &lt; threads; threadId++) {
          threadPool.submit(new TestWorker(this, currGen));
      }
  }
  
  public synchronized boolean get(long generation) {        
      if (generation != currGen) {
          return false;
      }
  
      if (this.left == 0) {
          return false;
      }
  
      this.left--;
  
      return true;
  }
  
  public synchronized void put(long generation) {           
      if (generation != currGen) {
          return;
      }
  
      this.done++;
  
      if (this.done == this.size) {
          this.run();
      }
  }
  }
  </code></pre>
  
  <p>Then, this class is run in the main method of my program by:</p>
  
  <pre><code>    Tester tester = new Tester(30 * 400, 30);
  
      tester.run();
  </code></pre>
  
  <p><strong>Observed behaviour:</strong> The output consists of Starting 1\n [...] Starting 1034\n  After that the process is still alive, but no more lines are printed. The number of threads for my process is 31014 at the moment of deadlock. The experiment was carried out at a 24-cores machine.</p>
  
  <p><strong>Expected behaviour:</strong> The process should keep printing Starting k\n for k = 1, 2, ... forever or throw an <code>OutOfMemoryError</code> caused by too many ThreadPools created.</p>
  
  <p>The presented example has limited debugging. At some moment I had more printf commands, they implied that the deadlock occurs when all the created threads of the current generation have called their <code>publish()</code> methods but the <code>process()</code> method is not called by the EDT.</p>