为什么要在Java中使用嵌套的InvokeLater调用? [英] Why would you use nested InvokeLater calls in Java?

查看:140
本文介绍了为什么要在Java中使用嵌套的InvokeLater调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构一些运行多阶段进程的代码。每个步骤都在嵌套的 java.awt.EventQueue.invokeLAter ....调用中。看起来有点像这样:

I'm refactoring some code that runs a multi-stage process. Each step is inside a nested java.awt.EventQueue.invokeLAter.... call. It looks a little like this:

   import java.awt.EventQueue;


public class NestedInvokeLater {

    /**
     * @param args
     */
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                changeTabPanel();
                copySomeFiles();
                enableNextButton1();
                upDateProgressBar(10);
                java.awt.EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        readInFiles();
                        doSomethingToFiles();
                        upDateProgressBar(15);
                        java.awt.EventQueue.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                doSomethingElse();
                                upDateProgressBar(100);

                            }
                        });
                    }
                });

            }

        });

    };
}

我是Java的新手,我不明白嵌套这些调用将作业添加到EDT中,我也不是100%有信心摆弄这些调用。我想我明白了 invokeLater 调用的作用,以及每个步骤的作用。如果这种理解错误,请纠正我:

I am new enough at Java that I don't get the point of nesting these calls to add 'jobs' to the EDT, and I'm not 100% confident with fiddling with these calls either. I think I understand what the invokeLater call does, and what each step does. Please correct me if this understanding is wrong:

invokeLater 用于将一些调用添加到作业列表中在Event Dispatch线程中完成。然后,Java处理每次调用的时间/方式,确保EDT和GUI不会因为它在后台执行作业而锁定。

invokeLater is used to add some invocation to the list of jobs to be done in the Event Dispatch thread. Java then deals with when/how each invocation is done, ensuring that the EDT and in turn the GUI doesn't lock as it performs jobs 'in the background'.

嵌套这些调用告诉我,我们应该排队一组作业,其中一个是排队的东西,这会排队一些工作....其中一个是排队的东西。但是,一旦上一个作业完成,第一个内部调用才会排队。一切都顺序发生(这符合我对整个过程的理解),但我不明白为什么你会使用嵌套请求来排队作业。如果我从头开始编写这个函数,我会简单地为每个调用创建函数并依次调用它们。

Nesting these calls says to me that we should queue a set of jobs, one of which is to queue something, which will queue some jobs....one of which is to queue something. But the first inner invocation is only ever queued once the previous job is done. Everything occurs sequentially (this is in line of my understanding of the whole process), but I don't see why you would use nested requests to queue jobs to do so. I would have, if I was writing this from scratch, have simply created functions for each invocation and called them in turn.

我认识到,我只是Java的新手我我可能错过了一些巨大的东西,这使得这种嵌套很重要但是没有这方面的文档,并且代码中没有关于嵌套的评论。

I recognise, being only a novice at Java I am probably missing something huge that makes this nesting important. But there is no documentation of this, and no commenting in the code about the nesting.

我缺少什么?什么,如果有什么是这个代码中的要点?

What am I missing? What, if anything is the point in this code?

推荐答案

这么多嵌套没有意义调用。它基于良好的意图,但实施得很糟糕。

There is no point in doing so many nested invocations. It is based on a good intention, but it's badly implemented.

如果你想这样做,请使用 SwingWorker

If you want to do this properly, use a SwingWorker.

SwingWorker 的文档有一个很好的例子,说明如何在应用程序的后台实现执行多个任务( PrimeNumbersTask 类显示在那里。)

编辑:以下是您在案例中应该使用SwingWorker做的一个示例。

Here's an example of what you should do with SwingWorker in your case.

class SequentialInvoker extends SwingWorker<Void, Integer> {
    @Override
    public void doInBackground() {

        changeTabPanel();
        copySomeFiles();
        enableNextButton1();
        setProgress(10);

        readInFiles();
        doSomethingToFiles();
        setProgress(15);

        doSomethingElse();
        setProgress(100);
    }
}

要在进度条上实际显示进度,请看看下面的代码,复制自 SwingWorker 文档:

To actually show the progress on a progress bar, take a look at the following code, copied from the SwingWorker documentation:

JTextArea textArea = new JTextArea();
JProgressBar progressBar = new JProgressBar(0, 100);
SequentialInvoker task = new SequentialInvoker();
task.addPropertyChangeListener(
    new PropertyChangeListener() {
        public  void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equals(evt.getPropertyName())) {
                progressBar.setValue((Integer)evt.getNewValue());
            }
        }
    }); 

使用此代码,您的进度条将显示进度 SwingWorker 有效。

With this code, your progress bar will show the progress as the SwingWorker works.

这篇关于为什么要在Java中使用嵌套的InvokeLater调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆