001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.configuration2.sync; 018 019import java.util.concurrent.locks.ReadWriteLock; 020import java.util.concurrent.locks.ReentrantReadWriteLock; 021 022/** 023 * <p> 024 * A special implementation of {@code Synchronizer} based on the JDK's {@code ReentrantReadWriteLock} class. 025 * </p> 026 * <p> 027 * This class manages a {@code ReadWriteLock} object internally. The methods of the {@code Synchronizer} interface are 028 * delegated to this lock. So this class behaves in the same way as documented for {@code ReentrantReadWriteLock}. 029 * </p> 030 * <p> 031 * Using this {@code Synchronizer} implementation is appropriate to make configuration objects thread-safe. This means 032 * that multiple threads can read configuration data in parallel; if one thread wants to update the configuration, this 033 * happens with an exclusive lock. 034 * </p> 035 * 036 * @since 2.0 037 */ 038public class ReadWriteSynchronizer implements Synchronizer { 039 040 /** 041 * Returns a new default lock object which is used if no lock is passed to the constructor. 042 * 043 * @return the new default lock object 044 */ 045 private static ReadWriteLock createDefaultLock() { 046 return new ReentrantReadWriteLock(); 047 } 048 049 /** The lock object used by this Synchronizer. */ 050 private final ReadWriteLock lock; 051 052 /** 053 * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with a lock object of type 054 * {@code ReentrantReadWriteLock}. 055 */ 056 public ReadWriteSynchronizer() { 057 this(null); 058 } 059 060 /** 061 * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with the given lock object. This 062 * constructor can be used to pass a lock object which has been configured externally. If the lock object is 063 * <strong>null</strong>, a default lock object is created. 064 * 065 * @param l the lock object to be used (can be <strong>null</strong>) 066 */ 067 public ReadWriteSynchronizer(final ReadWriteLock l) { 068 lock = l != null ? l : createDefaultLock(); 069 } 070 071 @Override 072 public void beginRead() { 073 lock.readLock().lock(); 074 } 075 076 @Override 077 public void beginWrite() { 078 lock.writeLock().lock(); 079 } 080 081 @Override 082 public void endRead() { 083 lock.readLock().unlock(); 084 } 085 086 @Override 087 public void endWrite() { 088 lock.writeLock().unlock(); 089 } 090}