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       * Returns a new default lock object which is used if no lock is passed to the constructor.
41       *
42       * @return the new default lock object
43       */
44      private static ReadWriteLock createDefaultLock() {
45          return new ReentrantReadWriteLock();
46      }
47  
48      /** The lock object used by this Synchronizer. */
49      private final ReadWriteLock lock;
50  
51      /**
52       * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with a lock object of type
53       * {@code ReentrantReadWriteLock}.
54       */
55      public ReadWriteSynchronizer() {
56          this(null);
57      }
58  
59      /**
60       * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with the given lock object. This
61       * constructor can be used to pass a lock object which has been configured externally. If the lock object is
62       * <strong>null</strong>, a default lock object is created.
63       *
64       * @param l the lock object to be used (can be <strong>null</strong>)
65       */
66      public ReadWriteSynchronizer(final ReadWriteLock l) {
67          lock = l != null ? l : createDefaultLock();
68      }
69  
70      @Override
71      public void beginRead() {
72          lock.readLock().lock();
73      }
74  
75      @Override
76      public void beginWrite() {
77          lock.writeLock().lock();
78      }
79  
80      @Override
81      public void endRead() {
82          lock.readLock().unlock();
83      }
84  
85      @Override
86      public void endWrite() {
87          lock.writeLock().unlock();
88      }
89  }