All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.hbase.zookeeper.InstancePending Maven / Gradle / Ivy

There is a newer version: 4.15.0-HBase-1.5
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hadoop.hbase.zookeeper;

import java.util.concurrent.CountDownLatch;

/**
 * Placeholder of an instance which will be accessed by other threads
 * but is not yet created. Thread safe.
 */
class InstancePending {
  // Based on a subtle part of the Java Language Specification,
  // in order to avoid a slight overhead of synchronization for each access.

  private final CountDownLatch pendingLatch = new CountDownLatch(1);

  /** Piggybacking on {@code pendingLatch}. */
  private InstanceHolder instanceHolder;

  private static class InstanceHolder {
    // The JLS ensures the visibility of a final field and its contents
    // unless they are exposed to another thread while the construction.
    final T instance;

    InstanceHolder(T instance) {
      this.instance = instance;
    }
  }

  /**
   * Returns the instance given by the method {@link #prepare}.
   * This is an uninterruptible blocking method
   * and the interruption flag will be set just before returning if any.
   */
  T get() {
    InstanceHolder instanceHolder;
    boolean interrupted = false;

    while ((instanceHolder = this.instanceHolder) == null) {
      try {
        pendingLatch.await();
      } catch (InterruptedException e) {
        interrupted = true;
      }
    }

    if (interrupted) {
      Thread.currentThread().interrupt();
    }
    return instanceHolder.instance;
  }

  /**
   * Associates the given instance for the method {@link #get}.
   * This method should be called once, and {@code instance} should be non-null.
   * This method is expected to call as soon as possible
   * because the method {@code get} is uninterruptibly blocked until this method is called.
   */
  void prepare(T instance) {
    assert instance != null;
    instanceHolder = new InstanceHolder(instance);
    pendingLatch.countDown();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy