When to use SwingUtilies.invokeAndWait/invokeLater

后端 未结 4 1863
半阙折子戏
半阙折子戏 2021-02-06 01:42

I read somewhere that for any thread that affects the visuals of the gui it should be ran in the EDT using SwingUtilities.invokeAndWait/invokeLater

For a basic gui, is i

4条回答
  •  清歌不尽
    2021-02-06 02:07

    The short answer to your question is: yes, even calling setVisible should happen on the EDT. To find out whether the current thread is the EDT, you can use the EventQueue#isDispatchThread method

    Some reference links:

    • Multithreaded Swing Applications
    • Threads and Swing
    • Concurrency in Swing

    Edit: after reading the links I provided, it seems some of the articles on the Oracle site are outdated as in they still document you can create Swing components on another thread. There is a stackoverflow question on this which contains some nice answers and links to blogposts and articles about the 'new' policy (new as in a few years old)

提交回复
热议问题