完成所有其他过程后,使用InvokeLater显示框架

时间:2011-11-07 15:41:46

标签: java multithreading swing frame event-dispatch-thread

我担心这个有点棘手,因为我无法在我为这个问题编写的例子中重现这个问题(下面的例子非常有效)。希望有人可能对实际应用可能出现的问题有所了解。 我写了一个应用程序,执行几个长文本操作。每个操作都在自己的线程中完成。有一个框架由线程更新,让用户看到一切都在进展。

问题是框架显示的所有更新只有在所有线程完成其作业后才会发送给它。
我已将整个应用程序简化为下面的代码,但正如我所说的问题是它在这里工作。任何想法都非常受欢迎。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {

    private MyFrame frame;
    private ExecutorService executorService;

    public static void main(String[] args) throws InterruptedException {
        Main main = new Main();
        main.startProcess();
    }

    public void startProcess() throws InterruptedException {
        // Initialize the frame
        frame = new MyFrame();
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(true);
            }
        });

        // Initialize executorService for 3 threads and also 6 runnables
        executorService = Executors.newFixedThreadPool(3);
        MyRunnable runnable;
        for(int i = 0; i < 6; i++) {
            runnable = new MyRunnable(this, i);
            executorService.execute(runnable);
        }

        // Start runnables
        executorService.shutdown();

        // Wait until all runnables are executed
        while (!executorService.isTerminated()) {
            Thread.sleep(10000);
        }

        // When all runnables are done close the frame
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(false);
            }
        });
    }

    // Update the frame display
    public synchronized void updateDisplay(final String update) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.updateDisplay(update);
            }
        });
    }

    private class MyFrame extends JFrame {
        private JPanel contentPane;
        private JLabel lblDisplay;

        public MyFrame() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);

            lblDisplay = new JLabel("Display");
            contentPane.add(lblDisplay, BorderLayout.CENTER);

            pack();
        }

        public void updateDisplay(String update) {
            lblDisplay.setText(update);
            pack();
        }
    }

    private class MyRunnable implements Runnable {
        private int id;
        private Main main;

        public MyRunnable (Main main, int id) {
            this.main = main;
            this.id = id;
        }

        @Override
        public void run() {
            for(int i = 0; i < 3; i++) {
                main.updateDisplay("Runnable " + id + " stepped " + i + " times.");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

SwingWorker的作业,只有一个JFrame,例如here

Runnable#Thread到GUI的输出必须包含在invokeLater()示例here

或调用SwingWorker from Executor