Violation of the rules for sending messages

We all know that we must perform all tasks related to the GUI from the event dispatch stream, and that strange errors can be introduced differently - I try to remember this rule, but I have to admit that I recently noticed a couple of places where I don’t know t

Is there a way to identify all violations of this rule so that they can be corrected? I saw that there is a corresponding rule here, but this is not like all cases for me. Even throwing an exception whenever a violation occurs would be nice, so I can fix it (or catch the exception and log a warning in case the user encounters the corresponding problem.)

What approaches do people usually take with them?

+4
source share
4 answers

One approach is to install a custom redraw manager that detects and registers when drawing is performed in a thread other than EDT. We use this approach in our project, adapted from the following blog: http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html . This will not detect all classes of EDT flow violations, but it is definitely much better than nothing.

Update:

As the commentator noted, the linked web page is no longer available. Here is my code adapted from the blog:

import javax.swing.JComponent; import javax.swing.RepaintManager; import javax.swing.SwingUtilities; import org.apache.log4j.Logger; import sun.awt.AppContext; public class DetectEdtViolationRepaintManager extends RepaintManager { private static final Logger LOGGER = Logger.getLogger( DetectEdtViolationRepaintManager.class); /** * Used to ensure we only print a stack trace once per abusing thread. May * be null if the option is disabled. */ private ThreadLocal alreadyWarnedLocal; /** * Installs a new instance of DetectEdtViolationRepaintManager which does not * warn repeatedly, as the current repaint manager. */ public static void install() { install(false); } /** * Installs a new instance of DetectEdtViolationRepaintManager as the current * repaint manager. * @param warnRepeatedly whether multiple warnings should be logged for each * violating thread */ public static void install(boolean warnRepeatedly) { AppContext.getAppContext().put(RepaintManager.class, new DetectEdtViolationRepaintManager(warnRepeatedly)); LOGGER.info("Installed new DetectEdtViolationRepaintManager"); } /** * Creates a new instance of DetectEdtViolationRepaintManager. * @param warnRepeatedly whether multiple warnings should be logged for each * violating thread */ private DetectEdtViolationRepaintManager(boolean warnRepeatedly) { if (!warnRepeatedly) { this.alreadyWarnedLocal = new ThreadLocal(); } } /** * {@inheritDoc} */ public synchronized void addInvalidComponent(JComponent component) { checkThreadViolations(); super.addInvalidComponent(component); } /** * {@inheritDoc} */ public synchronized void addDirtyRegion(JComponent component, int x, int y, int w, int h) { checkThreadViolations(); super.addDirtyRegion(component, x, y, w, h); } /** * Checks if the calling thread is called in the event dispatch thread. * If not an exception will be printed to the console. */ private void checkThreadViolations() { if (alreadyWarnedLocal != null && Boolean.TRUE.equals(alreadyWarnedLocal.get())) { return; } if (!SwingUtilities.isEventDispatchThread()) { if (alreadyWarnedLocal != null) { alreadyWarnedLocal.set(Boolean.TRUE); } LOGGER.warn("painting on non-EDT thread", new Exception()); } } } 
+5
source

I just try to be careful myself. But you can install the code to check if a given piece of code was being executed in the send thread using SwingUtilities.isEventDispatchThread (), and do what you like if it is not (or if it is).

+4
source

“Look at substance” and “Feeling ” includes automatic control of EDT runtime . This can help catch EDT violations during testing. It throws an IllegalStateException when a violation is detected. This is good for testing.

+3
source

The fest swing module also includes an EDT test script you can install that throws an exception when it detects a violation.

0
source

Source: https://habr.com/ru/post/1386629/


All Articles