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 */
017 package org.apache.commons.lang3.concurrent;
018
019 import java.util.concurrent.atomic.AtomicReference;
020
021 /**
022 * <p>
023 * A specialized {@code ConcurrentInitializer} implementation which is similar
024 * to {@link AtomicInitializer}, but ensures that the {@link #initialize()}
025 * method is called only once.
026 * </p>
027 * <p>
028 * As {@link AtomicInitializer} this class is based on atomic variables, so it
029 * can create an object under concurrent access without synchronization.
030 * However, it implements an additional check to guarantee that the
031 * {@link #initialize()} method which actually creates the object cannot be
032 * called multiple times.
033 * </p>
034 * <p>
035 * Because of this additional check this implementation is slightly less
036 * efficient than {@link AtomicInitializer}, but if the object creation in the
037 * {@code initialize()} method is expensive or if multiple invocations of
038 * {@code initialize()} are problematic, it is the better alternative.
039 * </p>
040 * <p>
041 * From its semantics this class has the same properties as
042 * {@link LazyInitializer}. It is a "save" implementation of the lazy
043 * initializer pattern. Comparing both classes in terms of efficiency is
044 * difficult because which one is faster depends on multiple factors. Because
045 * {@code AtomicSafeInitializer} does not use synchronization at all it probably
046 * outruns {@link LazyInitializer}, at least under low or moderate concurrent
047 * access. Developers should run their own benchmarks on the expected target
048 * platform to decide which implementation is suitable for their specific use
049 * case.
050 * </p>
051 *
052 * @since 3.0
053 * @version $Id: AtomicSafeInitializer.java 1088899 2011-04-05 05:31:27Z bayard $
054 * @param <T> the type of the object managed by this initializer class
055 */
056 public abstract class AtomicSafeInitializer<T> implements
057 ConcurrentInitializer<T> {
058 /** A guard which ensures that initialize() is called only once. */
059 private final AtomicReference<AtomicSafeInitializer<T>> factory =
060 new AtomicReference<AtomicSafeInitializer<T>>();
061
062 /** Holds the reference to the managed object. */
063 private final AtomicReference<T> reference = new AtomicReference<T>();
064
065 /**
066 * Get (and initialize, if not initialized yet) the required object
067 *
068 * @return lazily initialized object
069 * @throws ConcurrentException if the initialization of the object causes an
070 * exception
071 */
072 public final T get() throws ConcurrentException {
073 T result;
074
075 while ((result = reference.get()) == null) {
076 if (factory.compareAndSet(null, this)) {
077 reference.set(initialize());
078 }
079 }
080
081 return result;
082 }
083
084 /**
085 * Creates and initializes the object managed by this
086 * {@code AtomicInitializer}. This method is called by {@link #get()} when
087 * the managed object is not available yet. An implementation can focus on
088 * the creation of the object. No synchronization is needed, as this is
089 * already handled by {@code get()}. This method is guaranteed to be called
090 * only once.
091 *
092 * @return the managed data object
093 * @throws ConcurrentException if an error occurs during object creation
094 */
095 protected abstract T initialize() throws ConcurrentException;
096 }