[concurrency-interest] implementing a DB write-behind algorithm

Brian Goetz brian at quiotix.com
Mon May 1 17:15:41 EDT 2006


> One more question about the code sample: what is the
> [code]@GuardedBy("this")[/code] annotation responsible for? Mark that
> any operations on that field must be synched?

This is an annotation we introduce in the book.

@ThreadSafe
public class Counter {
   @GuardedBy("this") private int value;

   public synchronized int getNext() { return value++; }
}

In this case, we say value is guarded-by 'this' because we achieve 
thread safety by ensuring that whenever a thread accesses value, it must 
hold the Counter lock.  Its a way of documenting your synchroinzation 
policy, so that when the class is extended/maintained, you know how the 
locks are used to ensure thread safety and therefore are less likely to 
break it.


More information about the Concurrency-interest mailing list