开发者

AWT event thread interruption

I have code:

import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Test2 {

JFrame frame = null;
JPanel panel = null;
JButton button = null;
Task task = null;
Indicator indicator = null;
Runnable computation;

public static void main(String[] args) {
    new Test2().start();
}

public void start() {
    SwingUtilities.invokeLater(new Dialog());
}

private void process1() {
    int result = 0;

    for (int i=0; i<100000; i++) {
        result = (int) Math.ceil(++result + Math.sqrt(result));

        System.out.println("proc1 " + result);
    }
}

private void process2() {
    int result = 0;

    for (int i=0; i<100000; i++) {
        result = (int) Math.ceil(++result + Math.sqrt(result)*500);

        System.out.println("proc2 " + result);
    }
}

private class Computation implements Runnable {

    public void run() {

        process1();
        task.setProgress(2);
        process2();
        task.setProgress(3);
    }

}

private class Dialog implements Runnable {

    public Dialog() {
    }

    public void run() {
        frame = new JFrame("Test");
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(300, 200));
        frame.getContentPane().add(panel);
        button = new JButton("b1");
        panel.add(button);
        indicator = new Indicator();
        t开发者_Python百科ask = new Task();
        task.addObserver(indicator);

        frame.pack();
        frame.setVisible(true);

        computation = new Computation();
        SwingUtilities.invokeLater(computation);
    }
}

private class Task extends Observable {

    int progress;

    public Task() {

    }

    public void setProgress(int progress) {
        this.progress = progress;
        setChanged();
        notifyObservers();
    }

    public int getProgress() {
        return progress;
    }
}

private class Indicator implements Observer {

    @Override
    public void update(Observable arg0, Object arg1) {
        button.setText(((Task)arg0).getProgress()+"");
    }
}
}

So I have two time-consuming operations (process1 and process2). My aim is after process1 is complete, update swing-button (see task.setProgress method).

Problem consists in that update is performed after process1() and process2() are completed.


..update is performed after process1() and process2() are completed.

Don't perform long running tasks on the EDT, see Concurrency in Swing for details. One way to achieve that is to use a SwingWorker.


..if I use two SwingWorkers for performing process1() and process2(), then order of their execution is unpredictable. I need process2() follows by process1(). How I can obtain this?

Call both methods in the doInBackground() method of 1 SwingWorker, calling SwingWorker.setProgress(int) with the appropriate values at the appropriate times. E.G.

... doInBackground() {
    setProgress(0);
    process1();
    setProgress(50);
    process2();
    setProgress(100);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜