Java - SwingWorker 和 SwingUtilities.invokeLater() 之间的区别

发布于 2024-09-01 07:21:21 字数 1162 浏览 4 评论 0原文

SwingWorker 用于以下目的:

  • 用于在不同线程中运行长时间运行的任务,以防止 GUI 无响应 用于
  • 使用长时间运行的任务最后产生的结果来更新 GUI通过 done() 方法完成任务。
  • 用于在 publish()process() 方法的帮助下,使用任务生成和发布的中间结果不时更新 GUI。

SwingUtilities.invokeLater() 可以执行上述任务,如下所示:

  • 我们可以执行 ExecutorService.submit(,而不是从 EDT 执行 SwingWorker.execute() 方法new MyRunnable()) 因为它还会创建另一个可以执行长时间运行任务的线程。
  • 为了在任务结束时更新 GUI,我们可以将代码(写在 case1 的 done() 方法中)SwingUtilites.invokeLater(new RunnableToExecuteDoneMethodCode()) 放在最后的任务。
  • 为了在任务中间更新 GUI,我们可以将代码(写在 case1 的 process() 方法中)SwingUtilites.invokeLater(new RunnableToExecuteProcessMethodCode()) 放在该位置我们在 case1 中调用了 publish() 方法。

我问这个问题是因为问题中指定的问题 Java - SwingWorker - 我们可以从其他 SwingWorker 调用一个 SwingWorker 而不是 EDT 可以通过 SwingUtilities.invokeLater() 解决,但不能用 <代码>SwingWorker

SwingWorker is used for the following purposes:

  • For running long-running tasks in a different thread so as to prevent the GUI from being unresponsive
  • For updating GUI with the results produced by the long-running task at the end of the task through done() method.
  • For updating GUI from time to time with the intermediate results produced and published by the task with the help of publish() and process() methods.

SwingUtilities.invokeLater() can perform the above tasks as follows:

  • Instead of executing SwingWorker.execute() method from the EDT, we can execute ExecutorService.submit(new MyRunnable()) as it will also create another thread which can execute long-running task.
  • For updating GUI at the end of the task, we can put code (written in done() method of case1) SwingUtilites.invokeLater(new RunnableToExecuteDoneMethodCode()) at the end of the task.
  • For updating GUI in the middle of the task, we can put code (written in process() method of case1) SwingUtilites.invokeLater(new RunnableToExecuteProcessMethodCode()) at the place where we called publish() method in case1.

I am asking this question because the problem specified in question Java - SwingWorker - Can we call one SwingWorker from other SwingWorker instead of EDT can be solved by SwingUtilities.invokeLater() but can't be solved with SwingWorker

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滴情不沾 2024-09-08 07:21:25

1.6+ SwingWorker 类的一个重要特性是 doInBackground() 和 did() 之间的 EDT(事件调度线程)差异。您应该将 doInBackground() 视为 doWorkOutsideEDT(),将 did() 视为 doWorkInsideEDT()。运行此教学示例以查看不同之处。

    System.out.println("TID=" + Thread.currentThread().getId() + " (main)");
    final SwingWorker<String, String> x = new SwingWorker<String, String>() {
        @Override
        protected String doInBackground() throws Exception {
            final long tid = Thread.currentThread().getId();
            System.out.println("");
            System.out.println("TID=" + tid + " doInBackground() isEventDispatchThread=" + SwingUtilities.isEventDispatchThread());
            System.out.println("Long running code goes here.");
            return "";
        }

        @Override
        protected void done() {
            final long tid = Thread.currentThread().getId();
            System.out.println("");
            System.out.println("TID=" + tid + "          done() isEventDispatchThread=" + SwingUtilities.isEventDispatchThread());
            System.out.println("GUI updates/changes go here.");
        }
    };
    x.execute();
    x.get();

输出:

TID=1 (main)

TID=9 doInBackground() isEventDispatchThread=false
Long running code goes here.

TID=16          done() isEventDispatchThread=true
GUI updates/changes go here.

An important feature of the 1.6+ SwingWorker class is the EDT(Event Dispatch Thread) difference between doInBackground() and done(). You should think of doInBackground() as doWorkOutsideEDT() and done() as doWorkInsideEDT(). Run this instructional example to see the different.

    System.out.println("TID=" + Thread.currentThread().getId() + " (main)");
    final SwingWorker<String, String> x = new SwingWorker<String, String>() {
        @Override
        protected String doInBackground() throws Exception {
            final long tid = Thread.currentThread().getId();
            System.out.println("");
            System.out.println("TID=" + tid + " doInBackground() isEventDispatchThread=" + SwingUtilities.isEventDispatchThread());
            System.out.println("Long running code goes here.");
            return "";
        }

        @Override
        protected void done() {
            final long tid = Thread.currentThread().getId();
            System.out.println("");
            System.out.println("TID=" + tid + "          done() isEventDispatchThread=" + SwingUtilities.isEventDispatchThread());
            System.out.println("GUI updates/changes go here.");
        }
    };
    x.execute();
    x.get();

Output:

TID=1 (main)

TID=9 doInBackground() isEventDispatchThread=false
Long running code goes here.

TID=16          done() isEventDispatchThread=true
GUI updates/changes go here.
绳情 2024-09-08 07:21:24

SwingWorker 是一个辅助类——并不是说您需要使用它,但是使用它比手动完成相同的工作要简单和清晰得多。 (这也使得检查进度变得更容易。)请注意,它是在版本 6 中添加的——在此之前,有些人使用了 Swing 教程中定义的更简单的类,或者执行了与您提到的类似的步骤。

SwingWorker is a helper class -- it is not that you need to use it, but using it is much simpler and clearer than doing the same work by hand. (It also makes checking progress easier.) Note that it was added version 6 -- before then some people used a simpler class defined in the Swing Tutorial or did step similar to the ones you noted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文