View Javadoc
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    *     https://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  
19  import java.util.concurrent.locks.ReadWriteLock;
20  import java.util.concurrent.locks.ReentrantReadWriteLock;
21  
22  /**
23   * <p>
24   * A special implementation of {@code Synchronizer} based on the JDK's {@code ReentrantReadWriteLock} class.
25   * </p>
26   * <p>
27   * This class manages a {@code ReadWriteLock} object internally. The methods of the {@code Synchronizer} interface are
28   * delegated to this lock. So this class behaves in the same way as documented for {@code ReentrantReadWriteLock}.
29   * </p>
30   * <p>
31   * Using this {@code Synchronizer} implementation is appropriate to make configuration objects thread-safe. This means
32   * that multiple threads can read configuration data in parallel; if one thread wants to update the configuration, this
33   * happens with an exclusive lock.
34   * </p>
35   *
36   * @since 2.0
37   */
38  public class ReadWriteSynchronizer implements Synchronizer {
39  
40      /**
41       * Returns a new default lock object which is used if no lock is passed to the constructor.
42       *
43       * @return the new default lock object
44       */
45      private static ReadWriteLock createDefaultLock() {
46          return new ReentrantReadWriteLock();
47      }
48  
49      /** The lock object used by this Synchronizer. */
50      private final ReadWriteLock lock;
51  
52      /**
53       * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with a lock object of type
54       * {@code ReentrantReadWriteLock}.
55       */
56      public ReadWriteSynchronizer() {
57          this(null);
58      }
59  
60      /**
61       * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with the given lock object. This
62       * constructor can be used to pass a lock object which has been configured externally. If the lock object is
63       * <strong>null</strong>, a default lock object is created.
64       *
65       * @param l the lock object to be used (can be <strong>null</strong>)
66       */
67      public ReadWriteSynchronizer(final ReadWriteLock l) {
68          lock = l != null ? l : createDefaultLock();
69      }
70  
71      @Override
72      public void beginRead() {
73          lock.readLock().lock();
74      }
75  
76      @Override
77      public void beginWrite() {
78          lock.writeLock().lock();
79      }
80  
81      @Override
82      public void endRead() {
83          lock.readLock().unlock();
84      }
85  
86      @Override
87      public void endWrite() {
88          lock.writeLock().unlock();
89      }
90  }