简体   繁体   中英

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)"

I Asked that can write event loop like Node.js. But me say that Java already have some mechanism for it. That java can use hardware parallelization of operation. 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 )

看,没有线程

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);
            }
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM