简体   繁体   English

Java中如何实现线程并发

[英]How to achieve thread concurency in Java

I am working with a Java Swing application, where I have a main frame and multiple panels which I commute between by setting them visible or not and in the same time, I am instantiating a class which is running a while loop in the background.我正在使用 Java Swing 应用程序,在其中我有一个主框架和多个面板,我通过将它们设置为可见或不可见来在它们之间来回切换,同时,我正在实例化一个在后台运行 while 循环的类。 Now, the problem is: the panels don't appear unless that while loop ends, but I would like to let the user click some buttons while the while loops continues in the background, without even him knowing about that.现在,问题是:除非 while 循环结束,否则面板不会出现,但我想让用户在 while 循环在后台继续时单击一些按钮,甚至他都不知道。 Here is a small example of my code:这是我的代码的一个小例子:

        startPage.setVisible(false);
        lblError.setVisible(false);     
        new QuestionPage(Integer.parseInt(fieldUserID.getText()));

QuestionPage has a while loop going, and I would like to not freeze the whole application until that is finished, but to let that while loop run in the background. QuestionPage有一个 while 循环,我不想在完成之前冻结整个应用程序,而是让 while 循环在后台运行。 So far I tried doing 2 threads by extending the Thread class, but I am not sure if this is the right way to do it.到目前为止,我尝试通过扩展 Thread 类来做 2 个线程,但我不确定这是否是正确的方法。

[EDIT] [编辑]

Here is my NEXT button after using a SwingWorker in order to send in background the while loop which happens in QuestionPage class and to carry on with swing operations on the main frame这是我在使用 SwingWorker 之后的NEXT按钮,以便在后台发送发生在QuestionPage类中的 while 循环并在主框架上进行摆动操作

btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {                
            if (validateInput(fieldUserID.getText(), fieldAge.getText(), fieldSex.getSelectedItem().toString(), fieldExperience.getText())) {   
                startPage.setVisible(false);
                lblError.setVisible(false); 
                SwingWorker worker = new SwingWorker<Void, String>() {
                    @Override
                    public Void doInBackground() {
                        new QuestionPage(Integer.parseInt(fieldUserID.getText()));
                        return null;
                        }                           
                    };                             
            } else {
                lblError.setVisible(true);
            }               
        };                  
    });

[ANSWER] [回答]

The trick is to use a SwingWorker to send the long running task in the background and to also call execute() on it.诀窍是使用 SwingWorker 在后台发送长时间运行的任务,并在其上调用execute() Here is a minimal working example for a Start button event listener:这是Start按钮事件侦听器的最小工作示例:

 // Send long running task in background
                SwingWorker worker = new SwingWorker<Void, String>() {
                    @Override
                    public Void doInBackground() {
                        new QuestionPage(Integer.parseInt(fieldUserID.getText()));
                        return null;
                        }                           
                    };
                    worker.execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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