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 * http://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 /** The lock object used by this Synchronizer. */ 040 private final ReadWriteLock lock; 041 042 /** 043 * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with the given lock object. This 044 * constructor can be used to pass a lock object which has been configured externally. If the lock object is 045 * <b>null</b>, a default lock object is created. 046 * 047 * @param l the lock object to be used (can be <b>null</b>) 048 */ 049 public ReadWriteSynchronizer(final ReadWriteLock l) { 050 lock = l != null ? l : createDefaultLock(); 051 } 052 053 /** 054 * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with a lock object of type 055 * {@code ReentrantReadWriteLock}. 056 */ 057 public ReadWriteSynchronizer() { 058 this(null); 059 } 060 061 @Override 062 public void beginRead() { 063 lock.readLock().lock(); 064 } 065 066 @Override 067 public void endRead() { 068 lock.readLock().unlock(); 069 } 070 071 @Override 072 public void beginWrite() { 073 lock.writeLock().lock(); 074 } 075 076 @Override 077 public void endWrite() { 078 lock.writeLock().unlock(); 079 } 080 081 /** 082 * Returns a new default lock object which is used if no lock is passed to the constructor. 083 * 084 * @return the new default lock object 085 */ 086 private static ReadWriteLock createDefaultLock() { 087 return new ReentrantReadWriteLock(); 088 } 089}