groovy.transform
@Documented @Retention(value=SOURCE) @Target(value=METHOD) public @interface WithReadLock
WithWriteLock
to support read and write synchronization on a method.@WithReadLock
on your method. The method may be either an instance method or
a static method. The resulting method will allow multiple threads to read the information at the same time.
However, if some other method obtains a write lock, then this method will force callers to wait until the write is complete. 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
.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(); } } }
public abstract String value