簡體   English   中英

Java長計算而不會凍結GUI(在單線程中)

[英]Java long calculations without freezes GUI (in single thread)

在采訪中,有人問我:“如何在單個線程中執行許多計算而不會凍結進度條之類的GUI組件,或者能夠檢查另一用戶輸入?(只能執行一個線程)”

我問可以寫像Node.js這樣的事件循環。 但是我說Java已經有一些機制了。 那個java可以使用硬件並行化操作。 該任務可以使用哪些類或特殊詞?

因此,假設我們不能使用SwingWorker或Swing Timer ,因為它們創建了第二個線程來支持其操作,剩下的唯一選擇是使用SwingUtilities#invokeLater重復調用一個方法,該方法執行一個小的子集工作並在再次調用自身之前更新UI(使用SwingUtilities#invokeLater

看,沒有線程

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class NoMoreThreads {

    public static void main(String[] args) {
        new NoMoreThreads();
    }

    public NoMoreThreads() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        protected static final int MAX = 1000;

        private JProgressBar pb;
        private JButton start;
        private int count;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            start = new JButton("Let's get this party started");
            start.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    start.setEnabled(false);
                    calculate();
                }
            });

            pb = new JProgressBar(0, MAX);

            add(start, gbc);
            add(pb, gbc);
        }

        protected void calculate() {
            count++;
            if (count < MAX) {
                pb.setValue(count);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        calculate();
                    }
                });
            } else {
                start.setEnabled(true);
            }
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM