[JSR308] Concurrency annotations on blocks

Brian Goetz brian at quiotix.com
Sat Feb 3 00:45:20 EST 2007

> Intel's comment with their vote intrigued me:  that annotations on 
> blocks and loops might be useful for specifying atomicity/concurrency 
> assertions.  I believe most Java programmers don't have a full grasp of 
> concurrency issues, and am interested in any thoughts this group might 
> have regarding concurrency assertion ideas and what their annotations 
> might look like.  We can start with the ones in Java Concurrency in 
> Practice, since I doubt anyone would argue as to their importance.

The annotations laid out in JCiP are:
   @ThreadSafe (and its evil brother, @NotThreadSafe)

@ThreadSafe, @Immutable, and @NotThreadSafe declare properties of 
classes.  @GuardedBy declare relationships between data and locks.

I could imagine applying @ThreadSafe more generally to types, especially 
when polymorphism is involved.  For example, if you applied it to a 
method parameter:

   public void tweakList(@ThreadSafe List list) { ... }

then you could call tweakList with a Vector or a synchronized list but 
not a raw ArrayList.  Similarly, the synchronizedList() factory would 
return not just a List, but a @ThreadSafe list.  I think that 
annotations on types and local variables would be sufficient here.

I could also imagine javari-style annotations describing reference 
immutability.  Again, I think what this would require is annotations on 
types and local variables.

Some of the annotations we explored and dismissed for JCiP (not that 
they were not useful, but we wanted to keep the set down to the ones 
that would be used all the time) had to do with blocking behavior -- 
whether a method might execute a blocking wait, perform IO, etc.  These 
included: @MayBlock, @Async (returns before completing logical 
operation), @WithinThread (can only be called from a specific thread), 
@ReadOnly (does not modify object state).  There were a lot more, but 
they were generally finer-grained versions of these.  But still not 
seeing which of these would benefit from annotations on blocks.

More information about the JSR308 mailing list