Java SwingWorker não termina na conclusão da tarefa
Ok, então eu tenho brincado com o SwingWorker um pouco e tenho um código simplificado para atualizar um gui, mas estou tendo problemas para descobrir como fazer com que o thread termine corretamente quando ele for concluído. Atualmente, está terminando apenas através da opção de parada. Como posso configurá-lo para também finalizar o thread corretamente quando terminar o processo? Atualmente, após oreturn null;
ele vai para a linha de pacote e trava.
Meu código está abaixo:
package concurrency;
import java.util.List;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class PBTest extends JFrame implements ActionListener {
private final GridBagConstraints constraints;
private final JProgressBar pb, pbF;
private final JButton theButton;
private PBTask pbTask;
private JProgressBar makePB() {
JProgressBar p = new JProgressBar(0,100);
p.setValue(0);
p.setStringPainted(true);
getContentPane().add(p, constraints);
return p;
}
public PBTest() {
super("PBTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Make text boxes
getContentPane().setLayout(new GridBagLayout());
constraints = new GridBagConstraints();
constraints.insets = new Insets(3, 10, 3, 10);
pb = makePB();
pbF = makePB();
//Make buttons
theButton = new JButton("Start");
theButton.setActionCommand("Start");
theButton.addActionListener(this);
getContentPane().add(theButton, constraints);
//Display the window.
pack();
setVisible(true);
}
private static class UpdatePB {
private final int pb1, pb2;
UpdatePB(int pb1s, int pb2s) {
this.pb1 = pb1s;
this.pb2 = pb2s;
}
}
private class PBTask extends SwingWorker<Void, UpdatePB> {
@Override
protected Void doInBackground() {
int prog1 = 0;
int prog2 = 0;
Random random = new Random();
while (prog2 < 100) {
if(prog1 >= 100) {
prog1 = 0;
}
//Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {}
//Make random progress.
prog1 += random.nextInt(10);
prog2 += random.nextInt(5);
publish(new UpdatePB(prog1, prog2));
}
return null;
}
@Override
protected void process(List<UpdatePB> pairs) {
UpdatePB pair = pairs.get(pairs.size() - 1);
pb.setValue(pair.pb1);
pbF.setValue(pair.pb2);
}
}
public void actionPerformed(ActionEvent e) {
if ("Start" == e.getActionCommand() && pbTask == null) {
theButton.setText("Stop");
theButton.setActionCommand("Stop");
(pbTask = new PBTask()).execute();
} else if ("Stop" == e.getActionCommand()) {
theButton.setText("Start");
theButton.setActionCommand("Start");
pbTask.cancel(true);
pbTask = null;
} else {
alertMsg("Thread still running.");
}
}
static void alertMsg(String theAlert) {
JOptionPane.showMessageDialog(null, theAlert);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PBTest();
}
});
}
}
Nota: isso é basicamente uma alteração do exemplo de "flipper" dos tutoriais Java ... Eu não sou muito um programador como um hacker de código (/ sad face /, lol), no momento, então eu sou do tipo em uma perda quanto a onde ir em seguida.
De qualquer forma, o código funciona como planejado até terminar. Eu tentei adicionar odone()
método, mas nunca tenta executá-lo, ele sempre apenas vai para a linha de pacote (ao percorrer o depurador) e trava. Eu deveria retornar um valor diferente de null ou algo assim?
Agradecemos antecipadamente por qualquer ajuda!