ReadWriteSynchronizer.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.sync;

  18. import java.util.concurrent.locks.ReadWriteLock;
  19. import java.util.concurrent.locks.ReentrantReadWriteLock;

  20. /**
  21.  * <p>
  22.  * A special implementation of {@code Synchronizer} based on the JDK's {@code ReentrantReadWriteLock} class.
  23.  * </p>
  24.  * <p>
  25.  * This class manages a {@code ReadWriteLock} object internally. The methods of the {@code Synchronizer} interface are
  26.  * delegated to this lock. So this class behaves in the same way as documented for {@code ReentrantReadWriteLock}.
  27.  * </p>
  28.  * <p>
  29.  * Using this {@code Synchronizer} implementation is appropriate to make configuration objects thread-safe. This means
  30.  * that multiple threads can read configuration data in parallel; if one thread wants to update the configuration, this
  31.  * happens with an exclusive lock.
  32.  * </p>
  33.  *
  34.  * @since 2.0
  35.  */
  36. public class ReadWriteSynchronizer implements Synchronizer {
  37.     /**
  38.      * Returns a new default lock object which is used if no lock is passed to the constructor.
  39.      *
  40.      * @return the new default lock object
  41.      */
  42.     private static ReadWriteLock createDefaultLock() {
  43.         return new ReentrantReadWriteLock();
  44.     }

  45.     /** The lock object used by this Synchronizer. */
  46.     private final ReadWriteLock lock;

  47.     /**
  48.      * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with a lock object of type
  49.      * {@code ReentrantReadWriteLock}.
  50.      */
  51.     public ReadWriteSynchronizer() {
  52.         this(null);
  53.     }

  54.     /**
  55.      * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with the given lock object. This
  56.      * constructor can be used to pass a lock object which has been configured externally. If the lock object is
  57.      * <strong>null</strong>, a default lock object is created.
  58.      *
  59.      * @param l the lock object to be used (can be <strong>null</strong>)
  60.      */
  61.     public ReadWriteSynchronizer(final ReadWriteLock l) {
  62.         lock = l != null ? l : createDefaultLock();
  63.     }

  64.     @Override
  65.     public void beginRead() {
  66.         lock.readLock().lock();
  67.     }

  68.     @Override
  69.     public void beginWrite() {
  70.         lock.writeLock().lock();
  71.     }

  72.     @Override
  73.     public void endRead() {
  74.         lock.readLock().unlock();
  75.     }

  76.     @Override
  77.     public void endWrite() {
  78.         lock.writeLock().unlock();
  79.     }
  80. }