Groovy Documentation

groovy.transform
[Groovy] Annotation Type ThreadInterrupt

java.lang.Object
  groovy.transform.ThreadInterrupt

@Retention(RetentionPolicy.SOURCE)
@Target([ ElementType.METHOD, ElementType.TYPE])
@GroovyASTTransformationClass(["org.codehaus.groovy.transform.ThreadInterruptibleASTTransformation"])
@interface ThreadInterrupt

Allows "interrupt-safe" executions of scripts by adding Thread.currentThread().isInterrupted() checks on loops (for, while, do), the first statement of closures, and the first statement of methods.

This is especially useful when executing foreign scripts that you do not have control over. Inject this transformation into a script that you need to interrupt.

Annotating anything in a script will cause for loops, while loops, methods, and closures to make an isInterruptedCheck and throw a InterruptedException if the check yields true. The annotation by default will apply to any classes defined in the script as well. Annotated a class will cause (by default) all classes in the entire file ('Compilation Unit') to be enhanced. You can fine tune what is enhanced using the annotation parameters.

Extensive usage examples can be found in the unit test for this class. A smaller example is presented here. The following is sample usage of the annotation:

 @groovy.transform.ThreadInterrupt
 def scriptMethod() {
     4.times {
         println 'executing script method...'
     }
 }

 class MyClass {

   def myMethod() {
       for (i in (1..10)) {
           println 'executing method...'
       }
   }
 }

 scriptMethod()
 new MyClass().myMethod()
 

Which results in the following code being generated. Notice the checks and exceptions:
 public class script1290627909406 extends groovy.lang.Script {

     public java.lang.Object scriptMethod() {
         if (java.lang.Thread.currentThread().isInterrupted()) {
             throw new java.lang.InterruptedException('Execution Interrupted')
         }
         4.times({
             if (java.lang.Thread.currentThread().isInterrupted()) {
                 throw new java.lang.InterruptedException('Execution Interrupted')
             }
             this.println('executing script method...')
         })
     }
 }
 public class MyClass extends java.lang.Object {

     public java.lang.Object myMethod() {
         if (java.lang.Thread.currentThread().isInterrupted()) {
             throw new java.lang.InterruptedException('Execution Interrupted')
         }
         for (java.lang.Object i : (1..10)) {
             if (java.lang.Thread.currentThread().isInterrupted()) {
                 throw new java.lang.InterruptedException('Execution Interrupted')
             }
             this.println('executing method...')
         }
     }
 }

 this.scriptMethod()
 new MyClass().myMethod()
 
Authors:
Cedric Champeau
Hamlet D'Arcy
See Also:
TimedInterrupt
ConditionalInterrupt
Since:
1.8.0


Method Summary
boolean applyToAllClasses()

By default, annotating anything in a source file ('Compilation Unit') will trigger this transformation for all classes and scripts in that file.

boolean checkOnMethodStart()

By default an isInterrupted check is added to the start of all user-defined methods.

java.lang.Class thrown()

@default InterruptedException

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Method Detail

applyToAllClasses

boolean applyToAllClasses()
By default, annotating anything in a source file ('Compilation Unit') will trigger this transformation for all classes and scripts in that file. If you add the Annotation to an import statement, then all scripts and Classes will be enhanced. If you want to change this behavior then set applyToAllClasses to false. If you annotate a type then only that type will be augmented, not other types or the surrounding script. If you annotate a script, then any enclosed types will not be augmented. @default true
Returns:


checkOnMethodStart

boolean checkOnMethodStart()
By default an isInterrupted check is added to the start of all user-defined methods. To turn this off simply set this parameter to false. @default true
Returns:


thrown

java.lang.Class thrown()
@default InterruptedException


 

Groovy Documentation