如果我从其他线程引用对象到AWT线程,我是否需要同步?

发布于 2025-01-26 10:17:36 字数 869 浏览 2 评论 0原文

swingutilities.invokelater提供同步吗?

示例:

public class Threads {
    public static void main(String[] args) {
        new Thread(new Runnable() {     
            @Override
            public void run() {
                new Threads().test();
            }
        }).start();
    }
    
    List<Integer> list = new ArrayList<>(); 
    
    void test() {
        System.out.println(Thread.currentThread().toString());  
        
        for (int i=0; i<10000000; i++) {
            list.add(i);
        }
        
        SwingUtilities.invokeLater(() -> {                          
            System.out.println(Thread.currentThread().toString());
            list.forEach(System.out::println);  // list auto synchronized?      
        });
    }
}

当我从AWT线程中的列表中读取值时,将是list同步吗?

Does SwingUtilities.invokeLater provides synchronization?

Example:

public class Threads {
    public static void main(String[] args) {
        new Thread(new Runnable() {     
            @Override
            public void run() {
                new Threads().test();
            }
        }).start();
    }
    
    List<Integer> list = new ArrayList<>(); 
    
    void test() {
        System.out.println(Thread.currentThread().toString());  
        
        for (int i=0; i<10000000; i++) {
            list.add(i);
        }
        
        SwingUtilities.invokeLater(() -> {                          
            System.out.println(Thread.currentThread().toString());
            list.forEach(System.out::println);  // list auto synchronized?      
        });
    }
}

Would be list synchronized when I read values from the list in AWT thread?

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

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

发布评论

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

评论(2

谜泪 2025-02-02 10:17:36

swingutilities.invokelater提供同步吗?

否。它稍后才运行其参数。

当我从awt thread中的列表中读取值时,将被列表同步?

不,但不需要。它被发送到InvokElater创建和分配值后。它不会在以后的任何时间更改,因此在执行InvokeLater的代码时不会更改。

Does SwingUtilities.invokeLater provides synchronization?

No. It just runs its parameter later.

Would be list synchronized when I read values from the list in AWT thread?

No, but it doesn't need to be. It is sent to invokeLater after it was created and assigned with values. It doesn't change at any later time so it won't be changed while the code sent to invokeLater is executed.

无远思近则忧 2025-02-02 10:17:36

总结一些事实:

  • 一般而言,在
  • awt事件上,派遣线程EDT在awt事件上运行的所有事物
  • 都不保证执行eDT的顺序
  • EDT将耗时的工作派遣到工作线程(&gt; workes pattern)
  • EDT应该只执行与GUI相关的任务否则UI会冻结
  • swingutilities.invokelater基于awt.eventqueue.invokelater

因此,如果您有swingutilities.invokelater.invokelater并给予它并给予它可运行的(您的函数),该运行可运行将被放入disbachter线程(EDT)的事件标题中,该线程将使您的可运行的异步执行。

确切地说:

是的,您的代码作为InvokElater的一部分将在EDT上并行运行(如果是您的意思),该EDT与主线程不符。

不,它不在“另一个线程输入它”的情况下同步,因为它仅由EDT执行。

尽管这不是问题的一部分:不应以这种方式滥用EDT。取而代之的是,您应该将此任务放入工作线程中,并让EDT用于创建的内容:将与GUI相关的任务派遣到工作线程。

To summarize some facts:

  • in general Swing is not threadsafe
  • everything GUI-related runs on the awt Event Dispatching Thread
  • EDT does not guarantee the order of execution
  • EDT dispatches time-consuming work to worker threads (> worker pattern)
  • EDT should execute only small GUI-related tasks otherwise UI will freeze
  • SwingUtilities.invokeLater is based on awt.EventQueue.invokeLater

So if you have SwingUtilities.invokeLater and give it a Runnable (your function), this Runnable will be put into an event-queue for the dispachter thread (EDT) which will execute your Runnable asynchronously.

To be exact:

Yes, your code as part of invokeLater will run in parallel (if that is what you meant) on the EDT which runs asychronously to the main thread.

No, it is not synchronized in matter of "another thread entering it" because it is designated only to be executed by the EDT.

Although it is not part of the question: EDT should not be misused in that way. Instead you should put this task into a worker thread and let EDT be for what it has been created: dispatching GUI related tasks to worker threads.

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