Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4518

add support for @WithReadLock and @WithWriteLock declarative synchronization

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.8-beta-3
    • None
    • None
    • Patch

    Description

      add support for @WithReadLock and @WithWriteLock declarative synchronization

      As per the discussion here: http://groovy.329449.n5.nabble.com/new-transform-declarative-synchronization-td3271562.html

      I'll commit this myself, but am looking for feedback.

      This code:

      import groovy.transform.*;
      
      public class ResourceProvider {
      
          private final Map<String, String> data = new HashMap<String, String>();
      
          @WithReadLock
          public String getResource(String key) throws Exception {
                  return data.get(key);
          }
      
          @WithWriteLock
          public void refresh() throws Exception {
                  //reload the resources into memory
          }
      }
      

      Becomes this Code:

      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();
              }
          }
      }
      

      You can also specify your own lock, like so:

      import groovy.transforms.*;
      
      public class ResourceProvider {
      
          private final ReadWriteLock myLock = new ReentrantReadWriteLock();
          private final Map<String, String> data = new HashMap<String, String>();
      
          @WithReadLock('myLock')
          public String getResource(String key) throws Exception {
                  return data.get(key);
          }
      
          @WithWriteLock('myLock')
          public void refresh() throws Exception {
                  //reload the resources into memory
          }
      }
      

      In which case the code becomes:

      public class ResourceProvider {
      
          private final ReadWriteLock myLock = new ReentrantReadWriteLock();
          private final Map<String, String> data = new HashMap<String, String>();
      
          public String getResource(String key) throws Exception {
              myLock.readLock().lock();
              try {
                  return data.get(key);
              } finally {
                  myLock.readLock().unlock();
              }
          }
      
          public void refresh() throws Exception {
              myLock.writeLock().lock();
              try {
                  //reload the resources into memory
              } finally {
                  myLock.writeLock().unlock();
              }
          }
      }
      

      Attachments

        1. declarsync.patch
          12 kB
          Hamlet D'Arcy

        Activity

          People

            hamletdrc Hamlet D'Arcy
            hamletdrc Hamlet D'Arcy
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 4h
                4h
                Remaining:
                Remaining Estimate - 4h
                4h
                Logged:
                Time Spent - Not Specified
                Not Specified