CheckThread.org (Beta)

Using XML to Express Thread Policies

Instead of adding annotations to our Java classes, we can alternatively add a threadpolicy.xml file to our root top level package on the class path. Whether to use XML or annotations depends on the application and requirements.

When CheckThread runs, it indexes all files named "threadpolicy.xml" into a table for determining thread policies at analysis time.

  • The file threadpolicy.xml must be on the classpath and in a root directory, either in a file system directory or a jar file. Placing threadpolicy.xml within a java package such as com/mysite/threadpolicy.xml will not work.

  • Regular expressions can only be used on XML attributes called "pattern".

  • Multiple threadpolicy.xml files can be used. Typically one per jar file or directory.

Example XML

The following sample threadpolicy.xml file tells CheckThread that the method 'somemethod1' is ThreadSafe and any method that starts with "get" is NotThreadSafe.

<?xml version="1.0"?>
<!-- Sample Java Thread Policy Descriptor -->

<threadpolicy version="1.0">

    <!-- Example 1: Apply this policy to the 
         Java class named "com.mypackage.SomeClass" -->
    <class name="com.mypackage.SomeClass">
	
        <!-- Annotate someMethod1() as ThreadSafe -->
        <policy type="ThreadSafe">
              <method pattern="someMethod1" /> 
        </policy>
		
        <!-- Using a regular expression, annotate all methods 
             that start with "get" as NotThreadSafe -->
        <policy type="NotThreadSafe">
              <method pattern="get.*" />
        </policy>
		
        <!-- Annotate someMethod3() as ThreadConfined -->
        <policy type="ThreadConfined" threadname="ThreadName.EDT">
              <method pattern="someMethod3" />
        </policy>
    </class> 	
</threadpolicy>

This example uses regular expressions to apply a policy to multiple packages

<?xml version="1.0"?>
<!-- Sample Java Thread Policy Descriptor -->

<threadpolicy version="1.0"> 
    <!-- Example 2: Apply this policy to any Java class under the package named "foo.bar" -->
    <class pattern="foo.bar.*">
        
        <!-- Annotate all methods that start with "get" as ThreadSafe -->
        <policy type="ThreadSafe">
              <method pattern="get.*" /> 
        </policy>
    </class> 
	
</threadpolicy>
©2009 CheckThread