解决方法
FEST框架有一个工具来检测来自EDT的Swing使用情况.它基本上是一个RepaintManager安装.该框架面向测试,但RepaintManager可以在部署时使用.
>见FEST – Swing’s Event Dispatch Thread
或者,要检查所有方法,如getters和setter只能在EDT上访问,您可以使用AspectJ和加载时编织将SwingUtilities.isDisaptchThread()建议添加到您的swing组件(和JDK Swing组件)上的每个方法. )
@Aspect public class EDTCheck { @Pointcut("call (* javax.swing..*+.*(..)) || " + "call (javax.swing..*+.new(..))") public void swingMethods() {} @Pointcut("call (* com.mystuff.swing..*+.*(..)) || " + "call (com.mystuff.swing..*+.new(..))") public void mySwingMethods() {} @Pointcut("call (* javax.swing..*+.add*Listener(..)) || " + "call (* javax.swing..*+.remove*Listener(..)) || " + "call (void javax.swing.JComponent+.setText(java.lang.String))") public void safeMethods() {} @Before("(swingMethods() || mySwingMethods()) && !safeMethods()") public void checkCallingThread(JoinPoint.StaticPart thisJoinPointStatic) { if(!SwingUtilities.isDispatchThread()) { System.out.println( "Swing single thread rule violation: " + thisJoinPointStatic); Thread.dumpStack(); // or you might throw an unchecked exception } } }
(从文章稍微修改 – 添加mySwingMethods切入点,并使用SwingUtiliites.isDispatchThread()实际上与EventQueue.isDispatchThread()相同,但抽象更清洁.)
>见@L_403_2@