代码之家  ›  专栏  ›  技术社区  ›  Justin

如何检测Swing线程策略冲突

  •  10
  • Justin  · 技术社区  · 14 年前

    AOP_before(Method m, Object args[]) {
     if (!isEventDispatchThread(Thread.currentThread()) {
      logStack(new RuntimeException("violation!"));
     }
    
     invoke(m, args);
    }
    

    有人知道这样做的项目或实用程序吗?

    3 回复  |  直到 14 年前
        1
  •  8
  •   TofuBeer    14 年前

    我没用过这个,但这个 CheckThreadViolationRepaintManager 应该会成功的。

    它确实需要添加:

    RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
    

        2
  •  3
  •   Tobias P.    14 年前

    我发现一篇4年前的博客文章 some solutions ,但如果你找到一个能检测到最多EDT违规的,我会非常感兴趣。repaitmanager在检测所有违规行为时似乎不是防弹的。

        3
  •  3
  •   Epaga Alex Reynolds    12 年前

    RepaintManager.setCurrentManager(new RepaintManager() {
       public synchronized void addInvalidComponent( JComponent component ) {
         check( component );
         super.addInvalidComponent( component );
       }
       public void addDirtyRegion( JComponent component, int x, int y, int w, int h ) {
         check( component );
         super.addDirtyRegion( component, x, y, w, h );
       }
       private void check( JComponent c ) {
         if( !SwingUtilities.isEventDispatchThread() && c.isShowing() ) {
            new Throwable("EDT required!").printStackTrace();
         }
       }
    });
    

    只要在main方法中调用它,就可以在非EDT线程上更改组件时记录stacktraces。