事件调度线程的真正“同步”方法

时间:2013-05-28 23:30:23

标签: java swing synchronization

在方法上使用synchronized关键字一次只允许一个线程执行该方法,但EDT可以同时处理将在该方法中运行的多个“事件”。请参阅下面的示例代码以进行演示。单击测试按钮时,输出为:

0 before dialog, EDT=true
1 before dialog, EDT=true
(click OK button for 1 here)
1 after dialog, EDT=true
(click OK button for 0 here)
0 after dialog, EDT=true

我正在寻找的方法是一次只允许一个EDT事件在test()方法中处于活动状态,这样输出就可以

0 before dialog, EDT=true
(click OK button for 0 here)
0 after dialog, EDT=true
1 before dialog, EDT=true
(click OK button for 1 here)
1 after dialog, EDT=true

似乎有人必须先解决这个问题。我认为可以在方法的开头编写某种锁定对象,或者包装方法,但是懒惰,宁愿不重新发明轮子。

我的测试用例:

package test1;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class EDTSyncTest extends javax.swing.JFrame {
  private static final Object locker = new Object();

  private int counter;

  public EDTSyncTest() {
    initComponents();
  }

  private synchronized void test() {
    int l_id = counter++;
    logit("" + l_id + " before dialog, EDT=" + SwingUtilities.isEventDispatchThread());

    JOptionPane l_pane = new JOptionPane("test id " + l_id);
    JDialog l_diag = l_pane.createDialog(this, "test");
    l_diag.setModal(true);
    l_diag.setVisible(true);

    logit("" + l_id + " after dialog, EDT=" + SwingUtilities.isEventDispatchThread());
  }

  private void startTest() {
     new Delayer().execute();
     test();
  }

  private static void logit(String a_msg) {
    System.out.println(a_msg);
  }

  private class Delayer extends SwingWorker<Object, Object> {
    @Override
    protected Object doInBackground() throws Exception {
      Thread.sleep(2000);
      return null;
    }
    @Override
    protected void done() {
      test();
    }
  }

  private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new java.awt.FlowLayout());

    jButton1.setText("Test");
    jButton1.setName("jButton1"); // NOI18N
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });
    getContentPane().add(jButton1);

    pack();
  }

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    startTest();
  }                                        

  public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new EDTSyncTest().setVisible(true);
      }
    });
  }
  protected javax.swing.JButton jButton1;
}

2 个答案:

答案 0 :(得分:5)

好的,好的,这是我的答案,在millimoose的支持下指导:D

让我们从这些观察开始:

  • 只有一个 EDT主题
  • 线程只有一个执行上下文
  • 模态对话框阻止当前执行上下文
  • 始终在EDT上调用SwingWorker.done

因此,模态对话框无法阻止回放事件,否则会导致整个用户界面无响应;然后,它如何在不阻止挥动事件的情况下阻止?

模态对话框运行自己的事件调度循环。然后,代码执行调用图(在完成SwingWorker之后在EDT线程上运行完全)如下所示:

-> done (process FIRST done)
  -> 0/before
    -> modal dialog event loop (process NEXT done)
      -> 1/before
        -> modal dialog event loop (TOP DIALOG)
        <- OK PRESSED
      <- 1/after
    <- OK PRESSED
  <- 0/after 
<- back to normal EDT event loop

因此,模态对话框在运行时仍处理着摆动事件,但是以“递归”的方式。

答案 1 :(得分:1)

我刚刚弄清楚自己,但正如@millimoose所评论的那样:

  

test()方法没有被同时调用,只是递归调用。

稍微修改一下您的代码,您会看到:

logit("" + l_id + " before dialog, EDT="
    + SwingUtilities.isEventDispatchThread());
new Throwable().printStackTrace(System.out);

输出:

0 before dialog, EDT=true
java.lang.Throwable
    at EDTSyncTest.test(EDTSyncTest.java:22)
    at EDTSyncTest.startTest(EDTSyncTest.java:35)
    at EDTSyncTest.jButton1ActionPerformed(EDTSyncTest.java:75)
    at EDTSyncTest.access$1(EDTSyncTest.java:74)
    at EDTSyncTest$1.actionPerformed(EDTSyncTest.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    /* ... */
    at java.awt.EventDispatchThread.run(Unknown Source)
1 before dialog, EDT=true
java.lang.Throwable
    at EDTSyncTest.test(EDTSyncTest.java:22)
    at EDTSyncTest.access$0(EDTSyncTest.java:17)
    at EDTSyncTest$Delayer.done(EDTSyncTest.java:51)
    at javax.swing.SwingWorker$5.run(Unknown Source)
    /* ... */
    at java.awt.Dialog.setVisible(Unknown Source)
    at EDTSyncTest.test(EDTSyncTest.java:27)
    at EDTSyncTest.startTest(EDTSyncTest.java:35)
    at EDTSyncTest.jButton1ActionPerformed(EDTSyncTest.java:75)
    at EDTSyncTest.access$1(EDTSyncTest.java:74)
    at EDTSyncTest$1.actionPerformed(EDTSyncTest.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    /* ... */
    at java.awt.EventDispatchThread.run(Unknown Source)
1 after dialog, EDT=true
0 after dialog, EDT=true