Swing invokelater冻结

Tom*_*Tom 3 java concurrency swing event-dispatch-thread

我正在使用此代码从actionPerformed上的按钮直接调用invokeLater:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            int temp = (jComboBox1.getSelectedIndex() + 1);
        heavyProccesingFunction();
        }
   });
} 
Run Code Online (Sandbox Code Playgroud)

这仍然冻结了GUI.为什么?我没有使用invokelater函数得到相同的结果.

我应该用

Thread queryThread = new Thread() {
      public void run() { 
Run Code Online (Sandbox Code Playgroud)

代替?

编辑:

谢谢,应该使用新的线程.

Jon*_*eet 5

invokeLater仍然最终在调度程序线程上运行代码 - 稍后.目的invokeLater是允许后台线程在事件调度程序线程上发布工作.

听起来你应该确实创建另一个线程 - 或者使用线程池来获得相同类型的效果,或者SwingWorker例如.无论你做什么,都需要避免在事件调度程序线程上运行慢速方法.