com.denova.ui
Class SwingCheckerThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.denova.ui.SwingCheckerThread
All Implemented Interfaces:
java.lang.Runnable

public class SwingCheckerThread
extends java.lang.Thread

Replacement for Thread that checks many Swing methods for thread safety. It writes possible Swing thread errors to the log file {value #SwingCheckerThreadLogName}. The log file must exist before your program starts.

SwingCheckerThread checks Swing code at runtime. If can only check code that runs. That means you can get different results at different times and on different platforms. User choices, data differences, system configuration and races can all change the path your code takes at runtime. JVMs implement Swing and threading differently.

SwingCheckerThread only checks swing methods that use the RepaintManager. It will not check Swing methods that do not use the RepaintManager, such as methods that read values.

To make a Swing method call thread safe, use SwingUtilities.invokeLater(), or SwingUtilities.invokeAndWait(), or SwingWorker.

Warning: This check slows down your application. To disable Swing thread checking, delete the log file.

When you create a thread that may call into Swing, instead of

    extends Thread
 
use
    extends SwingCheckerThread
 
and instead of
    new Thread(...
 
use
    new SwingCheckerThread(...
 
If you are already in a thread and you want to check it, use
    SwingCheckerThread.startChecking();
 

The javadocs say some Swing methods are thread safe. Which ones actually are thread safe have changed over time. It is safer to assume none are thread safe.

Private class Checker derived from CheckingRepaintManager in Easily Find Swing Threading Issues, where the author says the code has an "unrestrictive license". Last modified: 2008-05-22


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static java.lang.String SwingCheckerThreadLogName
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SwingCheckerThread()
          SwingCheckerThread constructor.
SwingCheckerThread(java.lang.Runnable runnableClass)
          SwingCheckerThread constructor.
 
Method Summary
static Log getLog()
           
static boolean isEnabled()
           
static void setEnabled(boolean newEnabled)
           
static void startChecking()
          Starts checking swing calls for the current thread group.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SwingCheckerThreadLogName

public static java.lang.String SwingCheckerThreadLogName
Constructor Detail

SwingCheckerThread

public SwingCheckerThread()
SwingCheckerThread constructor.


SwingCheckerThread

public SwingCheckerThread(java.lang.Runnable runnableClass)
SwingCheckerThread constructor.

Parameters:
runnableClass - class that implements Runnable
Method Detail

startChecking

public static void startChecking()
Starts checking swing calls for the current thread group.


isEnabled

public static boolean isEnabled()

setEnabled

public static void setEnabled(boolean newEnabled)

getLog

public static Log getLog()