I wanted to try some ideas using SwingWorker , since I did not use it too much. Instead, I had a problem and I canโt understand what happened.
Here's a short SSCCE that demonstrates this problem (I know people here as SSCCE):
import javax.swing.SwingUtilities; import javax.swing.SwingWorker; public class SwingWorkerTest { public static void main (String[] args) { SwingUtilities.invokeLater (new Runnable () { @Override public void run () { new MySwingWorker (500).execute (); new MySwingWorker (900).execute (); new MySwingWorker (1200).execute (); } }); } } class MySwingWorker extends SwingWorker<Void, Void> { private int ms; public MySwingWorker (int ms) { this.ms = ms; } @Override protected Void doInBackground() { Thread t = Thread.currentThread (); for (int i = 0; i < 50; i++) { try { Thread.sleep (ms); } catch (InterruptedException e) { e.printStackTrace (); } System.out.println ("I am thread with " + ms + " sleep in iteration " + i + ": " + t.getName () + " (" + t.getId () + ")"); } return null; } }
So, my program should create 3 SwingWorkers that print a line on the screen, then sleep in the specified number of milliseconds, and then print the line again and sleep again, etc. I create a SwingWorker from a Swing thread, because otherwise they wonโt even start.
Thus, the expected result:
I am thread with 500 sleep in iteration 0: SwingWorker-pool-1-thread-1 (15) I am thread with 900 sleep in iteration 0: SwingWorker-pool-1-thread-2 (16) I am thread with 500 sleep in iteration 1: SwingWorker-pool-1-thread-1 (15) I am thread with 1200 sleep in iteration 0: SwingWorker-pool-1-thread-3 (17) I am thread with 500 sleep in iteration 2: SwingWorker-pool-1-thread-1 (15) I am thread with 900 sleep in iteration 1: SwingWorker-pool-1-thread-2 (16) I am thread with 500 sleep in iteration 3: SwingWorker-pool-1-thread-1 (15) I am thread with 1200 sleep in iteration 1: SwingWorker-pool-1-thread-3 (17) I am thread with 500 sleep in iteration 4: SwingWorker-pool-1-thread-1 (15) I am thread with 900 sleep in iteration 2: SwingWorker-pool-1-thread-2 (16) I am thread with 500 sleep in iteration 5: SwingWorker-pool-1-thread-1 (15) I am thread with 500 sleep in iteration 6: SwingWorker-pool-1-thread-1 (15) I am thread with 900 sleep in iteration 3: SwingWorker-pool-1-thread-2 (16) I am thread with 1200 sleep in iteration 2: SwingWorker-pool-1-thread-3 (17) I am thread with 500 sleep in iteration 7: SwingWorker-pool-1-thread-1 (15) .............
etc. for a total of 150 lines (3 work x 50 iterations for each)
Instead, the output I get is:
I am thread with 500 sleep in iteration 0: SwingWorker-pool-1-thread-1 (15) I am thread with 900 sleep in iteration 0: SwingWorker-pool-1-thread-2 (16) I am thread with 500 sleep in iteration 1: SwingWorker-pool-1-thread-1 (15)
What is it. Only 3 lines. After that, the program will exit. There is no stacktrace from this try catch .
one). Where is the worker with a sleep of 1200 ms? Actually, if I replace 1200 ms with 1000 ms, then this worker also prints 1 line ... yes, only one. Weird ...
2). Why do workers stop? (and I don't get 150 rows)
I ran this program using JRE 7 Update 11 on Windows 7 64-bit .
PS: I am sure that the error mentioned here has been fixed in this version of the JRE, since I get 2 different values โโ(15 and 16) as a stream identifier printed on the console. That was the first thing I suspected.