简体   繁体   English

Java长计算而不会冻结GUI(在单线程中)

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

At the interview, me was asked question: "How to perform many calculations in single thread without freezes GUI component like Progress Bar or that would be able to check another user input? (Can execute only one thread)" 在采访中,有人问我:“如何在单个线程中执行许多计算而不会冻结进度条之类的GUI组件,或者能够检查另一用户输入?(只能执行一个线程)”

I Asked that can write event loop like Node.js. 我问可以写像Node.js这样的事件循环。 But me say that Java already have some mechanism for it. 但是我说Java已经有一些机制了。 That java can use hardware parallelization of operation. 那个java可以使用硬件并行化操作。 What the classes or special words can used for this task? 该任务可以使用哪些类或特殊词?

So, assuming we can't use either SwingWorker or Swing Timer , as they create a second thread to support their operations, the only choice you're left with is to use SwingUtilities#invokeLater to repeatedly call a method, which performs a small subset of the work and updates the UI before calling itself again (using SwingUtilities#invokeLater ) 因此,假设我们不能使用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