在空的无限循环中检查选项 vs 做一些无限循环

Checking options in empty Infinite loop vs dosomething infite loop

public static void main(String... s) {
        StartUp obj = new StartUp();
          
        while(true) {
            //System.out.println("Option - " + option);
            if(option == 1) {
                option = 0;
                obj.setVisible(false);
                obj.dispose();
                new Test();
                break;
            }else if(option == 2) {
                option = 0;
                obj.setVisible(false);
                obj.dispose();
                new PWorld.Splash().load();
                break;
            }
        }
    }
    

我需要将 System.out.println("Option - " + option); 放入 while 循环中,否则程序会在 运行 StartUp obj = new StartUp();

后冻结

option 是 StartUp class 中的静态整数,由 Actionlistener 更改 option 中的值已被 ActionListener 更改,但 while 循环似乎不起作用。

但是如果我将 System.out.println("Option - " + option); 放在 while 循环中,它就可以工作。 为什么!

我正在使用这个 while 循环,因为 new PWorld.Splash().load();Thread.sleep(),如果从 ActionListener 调用 as in this answere 新的 JFrame 将不会被绘制(在 UI 线程中)其中有 Thread.

谢谢

如果初始输入时选项不是 1 或 2,此循环预期做什么?你只是无缘无故地燃烧 CPU 个周期,等待其他线程做某事。

添加 print 语句会注入一点 non-CPU-burning 延迟,在这种情况下,可能要设置 'option' 的线程会到达 运行.

(FWIW,如果您希望更改对其他线程可见,则可能需要将 'option' 声明为 volatile。

这不是一个好的设计。我无法充分说明上下文来告诉您 应该 做什么,但是需要某种适当的通知机制。但这应该可以回答您 'WHY?'.

的问题

您的问题是:

  • 您正在调用一个“紧密”循环,它占用 CPU 并阻止来自 运行ning 的其他代码。 System.out.println(...) 语句添加了减慢此循环的代码,从紧密循环的钳口中释放了 CPU,允许其他线程 运行,这就是您问题的起源。
  • 话虽如此,您的编码方法还是不好,因为您使用 while (true) 循环代替响应事件,而这正是 Swing GUI 的编码方式。
  • 您声明这是因为 while 循环中的一段代码调用了 Thread.sleep 并且如果在 Swing 事件线程(例如在 ActionListener 中)调用此代码,将会阻塞事件线程,冻结您的 GUI - 都是真的。
  • 但是你的解决方案是错误的。正确的解决方案不是在主方法的 while (true) 循环中调用它,而是从后台线程调用 Thread.sleep,例如在 doInBackground() 方法中 SwingWorker (link is to tutorial), or better still, to use a Swing Timer(同样,link 是教程)代替 Thread.sleep。这将允许您的代码在不阻塞 Swing 事件线程的情况下暂停一些代码。
  • 另一个选项,如果您需要显示对话框(子)window 是使用模态 JDialog 显示 window 同时阻止与主 GUI window 的交互,直到对话框 window 不再可见。

要获得更详细、更全面的解决方案,请再次考虑创建并发布包含您的问题的 Minimal, Reproducible Example 程序。

例如,这是我的最小可重现示例:

import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Window;
import javax.swing.*;

public class MinReproExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Startup startup = new Startup();
            startup.showStartUp();

            Option option = startup.getOption();
            if (option == Option.TEST) {
                JOptionPane.showMessageDialog(null, "Test selected", "Selection", JOptionPane.DEFAULT_OPTION);
            } else if (option == Option.PWORLD) {
                PWorld pworld = new PWorld();
                pworld.showSplash();
            }   
        });
    }
}
class Startup {
    private JDialog startupDialog;
    private Option option = null;
    
    public Startup() {
        ButtonGroup buttonGroup = new ButtonGroup();
        JPanel optionsPanel = new JPanel(new GridLayout(1, 0, 10, 10));
        optionsPanel.setBorder(BorderFactory.createTitledBorder("Options"));
        for (final Option op : Option.values()) {
            JRadioButton rBtn = new JRadioButton(op.getText());
            rBtn.setActionCommand(op.getText());
            optionsPanel.add(rBtn);
            buttonGroup.add(rBtn);
            rBtn.addActionListener(e -> {
                option = op;
                Window window = SwingUtilities.getWindowAncestor(optionsPanel);
                window.dispose();
            });
        }
        
        startupDialog = new JDialog(null, "Select Option", ModalityType.APPLICATION_MODAL);
        startupDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        startupDialog.add(optionsPanel);
        startupDialog.pack();
        startupDialog.setLocationRelativeTo(null);
    }
    
    public void showStartUp() {
        if (startupDialog != null) {
            startupDialog.setVisible(true);
        }
    }
    
    public Option getOption() {
        return option;
    }
}
class PWorld {
    private static final Color ROBINS_EGG_BLUE = new Color(0, 204, 204);
    private JDialog pworldSplashDialog;
    private JFrame mainPWorldFrame;

    public PWorld() {
        JLabel splashLabel = new JLabel("Splash Window", SwingConstants.CENTER);
        JPanel splashPanel = new JPanel(new GridBagLayout());
        splashPanel.add(splashLabel);
        splashPanel.setBackground(Color.PINK);
        splashPanel.setPreferredSize(new Dimension(300, 250));

        pworldSplashDialog = new JDialog(null, "Splash", ModalityType.MODELESS);
        pworldSplashDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        pworldSplashDialog.add(splashPanel);
        pworldSplashDialog.pack();
        pworldSplashDialog.setLocationRelativeTo(null);

        JLabel mainLabel = new JLabel("Main GUI Window", SwingConstants.CENTER);
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.add(mainLabel);
        mainPanel.setBackground(ROBINS_EGG_BLUE);
        mainPanel.setPreferredSize(new Dimension(500, 350));

        mainPWorldFrame = new JFrame("Main PWorld GUI");
        mainPWorldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPWorldFrame.add(mainPanel);
        mainPWorldFrame.pack();
        mainPWorldFrame.setLocationRelativeTo(null);

    }

    public void showSplash() {
        int timerDelay = 2000; // two second delay
        Timer timer = new Timer(timerDelay, e -> {
            if (pworldSplashDialog != null && pworldSplashDialog.isVisible()) {
                pworldSplashDialog.dispose();
                showMainPWorldFrame();
            }
        });
        timer.setRepeats(false);
        timer.start();

        pworldSplashDialog.setVisible(true);
    }

    private void showMainPWorldFrame() {
        mainPWorldFrame.setVisible(true);
    }
}
// options to choose from
enum Option {
    TEST("Test"), PWORLD("PWorld");
    private String text;

    private Option(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}