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

com.hazelcast.core.ICountDownLatch Maven / Gradle / Ivy

There is a newer version: 4.5.4
Show newest version
/*
 * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.core;

import java.util.concurrent.TimeUnit;

/**
 * ICountDownLatch is a backed-up distributed alternative to the
 * {@link java.util.concurrent.CountDownLatch java.util.concurrent.CountDownLatch}.
 * 

* ICountDownLatch is a cluster-wide synchronization aid * that allows one or more threads to wait until a set of operations being * performed in other threads completes. *

* There are a few differences compared to the {@link ICountDownLatch}: *

    *
  1. * the ICountDownLatch count can be reset using {@link #trySetCount(int)} after a countdown * has finished but not during an active count. This allows the same latch instance to be reused. *
  2. *
  3. * There is no await() method to do an unbound wait since this is undesirable in a distributed * application: for example, a cluster can split or the master and * replicas could all die. In most cases, it is best to configure an explicit timeout so you have the ability * to deal with these situations. *
  4. *
* Behaviour of {@link ICountDownLatch} under split-brain scenarios should be taken into account when using this * data structure. During a split, each partitioned cluster will either create a brand new and uninitialised (zero'd) * {@link ICountDownLatch} or it will continue to use the primary or back-up version. For example * it may be possible for both the back-up and primary to be resident in one cluster partition and for another to * be created as new in another side. In any of these cases the counter in the respective {@link ICountDownLatch} * may diverge. *

* When the split heals, Hazelcast performs a default largest cluster wins resolution or where clusters sizes are equal * a random winner is chosen. This can lead to situations where the {@ICountDown} is left in an unpredictable state, * and a countdown to zero may never be achieved. *

* If required, when using {@link ICountDownLatch} as an orchestration mechanism you should assess the state of the * orchestration outcome and the associated countdown actors after a split-brain heal has taken place, and take steps to * re-orchestrate if appropriate. * * Supports Quorum {@link com.hazelcast.config.QuorumConfig} since 3.10 in cluster versions 3.10 and higher. * */ public interface ICountDownLatch extends DistributedObject { /** * Causes the current thread to wait until the latch has counted down to * zero, or an exception is thrown, or the specified waiting time elapses. *

* If the current count is zero then this method returns immediately * with the value {@code true}. *

* If the current count is greater than zero, then the current * thread becomes disabled for thread scheduling purposes and lies * dormant until one of five things happen: *

    *
  • the count reaches zero due to invocations of the * {@link #countDown} method, *
  • this ICountDownLatch instance is destroyed, *
  • the countdown owner becomes disconnected, *
  • some other thread {@linkplain Thread#interrupt interrupts} * the current thread, or *
  • the specified waiting time elapses. *
* If the count reaches zero, then the method returns with the * value {@code true}. *

* If the current thread: *

    *
  • has its interrupted status set on entry to this method, or *
  • is {@linkplain Thread#interrupt interrupted} while waiting, *
* then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. *

* If the specified waiting time elapses then the value {@code false} * is returned. If the time is less than or equal to zero, the method * will not wait at all. * * @param timeout the maximum time to wait * @param unit the time unit of the {@code timeout} argument * @return {@code true} if the count reached zero, {@code false} * if the waiting time elapsed before the count reached zero * @throws InterruptedException if the current thread is interrupted * @throws IllegalStateException if the Hazelcast instance is shutdown while waiting * @throws NullPointerException if unit is null */ boolean await(long timeout, TimeUnit unit) throws InterruptedException; /** * Decrements the count of the latch, releasing all waiting threads if * the count reaches zero. *

* If the current count is greater than zero, then it is decremented. * If the new count is zero: *

    *
  • All waiting threads are re-enabled for thread scheduling purposes, and *
  • Countdown owner is set to {@code null}. *
* If the current count equals zero, then nothing happens. */ void countDown(); /** * Returns the current count. * * @return the current count */ int getCount(); /** * Sets the count to the given value if the current count is zero. *

* If count is not zero, then this method does nothing and returns {@code false}. * * @param count the number of times {@link #countDown} must be invoked * before threads can pass through {@link #await} * @return {@code true} if the new count was set, {@code false} if the current count is not zero * @throws IllegalArgumentException if {@code count} is negative */ boolean trySetCount(int count); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy