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 025 * {@code ReentrantReadWriteLock} class. 026 * </p> 027 * <p> 028 * This class manages a {@code ReadWriteLock} object internally. The methods of 029 * the {@code Synchronizer} interface are delegated to this lock. So this class 030 * behaves in the same way as documented for {@code ReentrantReadWriteLock}. 031 * </p> 032 * <p> 033 * Using this {@code Synchronizer} implementation is appropriate to make 034 * configuration objects thread-safe. This means that multiple threads can read 035 * configuration data in parallel; if one thread wants to update the 036 * configuration, this happens with an exclusive lock. 037 * </p> 038 * 039 * @since 2.0 040 */ 041public class ReadWriteSynchronizer implements Synchronizer 042{ 043 /** The lock object used by this Synchronizer. */ 044 private final ReadWriteLock lock; 045 046 /** 047 * Creates a new instance of {@code ReadWriteSynchronizer} and initializes 048 * it with the given lock object. This constructor can be used to pass a 049 * lock object which has been configured externally. If the lock object is 050 * <b>null</b>, a default lock object is created. 051 * 052 * @param l the lock object to be used (can be <b>null</b>) 053 */ 054 public ReadWriteSynchronizer(final ReadWriteLock l) 055 { 056 lock = l != null ? l : createDefaultLock(); 057 } 058 059 /** 060 * Creates a new instance of {@code ReadWriteSynchronizer} and initializes 061 * it with a lock object of type {@code ReentrantReadWriteLock}. 062 */ 063 public ReadWriteSynchronizer() 064 { 065 this(null); 066 } 067 068 @Override 069 public void beginRead() 070 { 071 lock.readLock().lock(); 072 } 073 074 @Override 075 public void endRead() 076 { 077 lock.readLock().unlock(); 078 } 079 080 @Override 081 public void beginWrite() 082 { 083 lock.writeLock().lock(); 084 } 085 086 @Override 087 public void endWrite() 088 { 089 lock.writeLock().unlock(); 090 } 091 092 /** 093 * Returns a new default lock object which is used if no lock is passed to 094 * the constructor. 095 * 096 * @return the new default lock object 097 */ 098 private static ReadWriteLock createDefaultLock() 099 { 100 return new ReentrantReadWriteLock(); 101 } 102}