可能重复:
How do I wait for a SwingWorker's doInBackground() method?有没有办法阻止主线程,直到完成一个回转工作?
我读过它真的不好的做法,这样做,但我认为它的最好办法,我达到我的目的...... 是有一种方法可以阻止主线程,直到swing工作者在下面的例子中完成为止?
public void flashNode(Node node, int noOfFlashes) {
GraphUpdater updater = new GraphUpdater(node, noOfFlashes);
updater.execute();
}
和SwingWorker
类
public class GraphUpdater extends SwingWorker<Integer, Integer> {
Node node;
int noOfFlashes;
public GraphUpdater(Node n, int noFlashes) {
node = n;
noOfFlashes = noFlashes;
}
@Override
protected Integer doInBackground() throws Exception {
Color origColor = node.getColor();
for (int i=0; i<noOfFlashes; i++) {
changeNodeColor(node, Color.WHITE);
Thread.sleep(500);
changeNodeColor(node, origColor);
Thread.sleep(500);
}
return 1;
}
}
在此先感谢
见http://stackoverflow.com/questions/4261666/how-do-i-wait -for-a-swingworkers-doinbackground-method –
但是,如果您想要首先阻止主UI线程,为什么要使用Swingworker呢?你可以在主线程中运行代码。 –