CheckThread.org

Annotation Reference

@ThreadSafe
This annotation tells CheckThread that a method and/or class can run on any thread. If a method marked with @ThreadSafe invokes a method marked with @ThreadConfined, CheckThread will throw an error due to conflicting thread policies. No thread policy error will be thrown if a method marked with @ThreadConfined invokes a method marked with @ThreadSafe. When using @ThreadSafe, it is up to the implementation of the method to ensure thread safety by using synchronization/locks, immutable data, or other techniques.

@ThreadConfined("some thread name")
This annotation tells checkthread that a method and/or class should only be called on the specified thread. The input to @ThreadConfined doesn't need to correspond to the runtime value of Thread.getName() but it is a good practice to do so for consistency. CheckThread will thrown an error if a method marked with @ThreadConfined(X) invokes a method marked with @ThreadConfined(Y) and X != Y.

@ThreadConfined(ThreadName.EDT)
Tells checkthread that a method and/or class should only be called on the event dispatch thread. This annotation is useful when developing Swing applications and you want to ensure, before running your code, that swing methods are not called off the event dispatch thread.

@ThreadConfined(ThreadName.MAIN)
Tells checkthread that a method and/or class should only be called on the main thread.

Notes on Using CheckThread Annotations

  • The best way to see how to use this annotation properly is to download and look at the examples.
  • CheckThread annotations can be added to any Java method or Java class.
  • CheckThread annotations can not be added to field definitions or other locations within Java code - by design.
  • Adding a thread policy annotation to every Java method is not required. That would be too verbose and make Java code look ugly. Instead, add an annotation at the class definition level.
  • If a Java method does not define a thread annotation, CheckThread will look to see if the parent class has a thread annotation.
  • You can override a class level thread policy annotation by adding annotations to individual methods on a case by case basis.
  • The argument to @ThreadConfined annotation defines the thread policy. The argument is an abitrary string. You can make this string whatever you want.
  • As a convenience, a thread policy string for the Event-Dispatch thread and Main thread are already defined.
  • To avoid name clashing, using your package name is recommended when naming a thread policy.
  • For enclosing classes (e.g. inner class or anonymous class), CheckThread will search the parent class if the enclosing class does not define an annotation.
  • CheckThread will automatically add a Main thread policy to Java code within static blocks - static blocks run on the main thread when loaded by a ClassLoader.
  • The thread policy for synthetic methods (methods generated by the compiler) will take on the underlying method or class thread policy.

 

©2008 CheckThread