|
Groovy 2.2.0 | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
java.lang.Object groovy.transform.WithWriteLock
@Retention(RetentionPolicy.SOURCE) @Target({ElementType.METHOD}) @GroovyASTTransformationClass("org.codehaus.groovy.transform.ReadWriteLockASTTransformation") public @interface WithWriteLock
This annotation is used in conjunction with WithReadLock to support read and write synchronization on a method.
To use this annotation, declare @WithWriteLock
on your method. The method may be either an instance method or
a static method. The resulting method will allow only one thread access to the method at a time, and will wait to access
the method until any other read locks have been released.
This annotation is a declarative wrapper around the JDK's java.util.concurrent.locks.ReentrantReadWriteLock
.
Objects containing this annotation will have a ReentrantReadWriteLock field named $reentrantLock
added to the class,
and method access is protected by the lock. If the method is static then the field is static and named $REENTRANTLOCK
.
The annotation takes an optional parameter for the name of the field. This field must exist on the class and must be of type ReentrantReadWriteLock.
To understand how this annotation works, it is convenient to think in terms of the source code it replaces. The following is a typical usage of this annotation from Groovy:
import groovy.transform.*; public class ResourceProvider { private final Map<String, String> data = new HashMap<String, String>();As part of the Groovy compiler, code resembling this is produced:@WithReadLock
public String getResource(String key) throws Exception { return data.get(key); }@WithWriteLock
public void refresh() throws Exception { //reload the resources into memory } }
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReadWriteLock; public class ResourceProvider { private final ReadWriteLock $reentrantlock = new ReentrantReadWriteLock(); private final Map<String, String> data = new HashMap<String, String>(); public String getResource(String key) throws Exception { $reentrantlock.readLock().lock(); try { return data.get(key); } finally { $reentrantlock.readLock().unlock(); } } public void refresh() throws Exception { $reentrantlock.writeLock().lock(); try { //reload the resources into memory } finally { $reentrantlock.writeLock().unlock(); } } }
Optional Element Summary | |
---|---|
java.lang.String |
value
|
Method Summary |
---|
Methods inherited from class Object | |
---|---|
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
Element Detail |
---|
public String value
Copyright © 2003-2013 The Codehaus. All rights reserved.