Por que o SwingWorker para inesperadamente?
Eu queria experimentar algumas ideias usandoSwingWorker
desde que eu não usei muito. Em vez disso, me deparei com um problema e não consigo descobrir o que está errado.
Aqui está um pequenoSSCCE que demonstra este problema (eu conheço pessoas aqui como SSCCEs):
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;
}
}
Assim, meu programa deve criar 3 SwingWorkers que imprimam uma linha na tela, depois durmam pelo número especificado de milissegundos e, em seguida, imprima a linha novamente e durma novamente.SwingWorker
s de dentro do segmento de Swing, porque senão eles nem começariam.
Portanto, a saída esperada é:
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)
.............
e assim por diante, para um total de 150 linhas (3 trabalhadores x 50 iterações para cada)
Em vez disso, a saída que recebo é:
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)
E é isso. Apenas 3 linhas Depois disso, o programa sai. Não há stacktrace em qualquer lugar daqueletry catch
quadra.
1). Onde está o trabalhador com o sono de 1200ms? Na verdade, se eu substituir 1200ms por 1000ms, esse trabalhador também imprime 1 linha ... sim, apenas um. Esquisito...
2). Por que os trabalhadores param? (e eu não tenho 150 linhas de coisas)
Eu corri este programa usandoJRE 7 Update 11 no Windows 7 de 64 bits.
PS: Eu tenho certeza queo bug mencionado aqui foi corrigido nesta versão do JRE, desde que eu recebo 2 valores distintos (15 e 16) como ID de thread impresso no console. Essa foi a primeira coisa que eu suspeitei.