org.apache.eventmesh.common.ResetCountDownLatch Maven / Gradle / Ivy
/*
* 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.eventmesh.common;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
/**
* ResetCountDownLatch can reset
*
* @see java.util.concurrent.CountDownLatch
*/
public class ResetCountDownLatch {
private final RestSync restSync;
public ResetCountDownLatch(int count) {
this.restSync = new RestSync(count);
}
/**
* Causes the current thread to wait until the latch has counted down to zero, unless the thread is {@linkplain Thread#interrupt interrupted}.
*
* If the current count is zero then this method returns immediately.
*
*
If the current count is greater than zero then the current
* thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen:
*
* - The count reaches zero due to invocations of the
* {@link #countDown} method; or
*
- Some other thread {@linkplain Thread#interrupt interrupts}
* the current thread.
*
*
* 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.
*
* @throws InterruptedException if the current thread is interrupted while waiting
*/
public void await() throws InterruptedException {
restSync.acquireSharedInterruptibly(1);
}
/**
* Causes the current thread to wait until the latch has counted down to zero, unless the thread is {@linkplain Thread#interrupt interrupted}, 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 three things happen:
*
* - The count reaches zero due to invocations of the
* {@link #countDown} method; or
*
- 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 and {@code false} if the waiting time elapsed before the count reached zero
* @throws InterruptedException if the current thread is interrupted while waiting
*/
public boolean await(long timeout, TimeUnit unit)
throws InterruptedException {
return restSync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
}
/**
* 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 then all waiting threads are re-enabled for thread scheduling purposes.
*
*
If the current count equals zero then nothing happens.
*/
public void countDown() {
restSync.releaseShared(1);
}
/**
* Returns the current count.
*
*
This method is typically used for debugging and testing purposes.
*
* @return the current count
*/
public int getCount() {
return restSync.getCount();
}
/**
* Reset the CountDownLatch
*/
public void reset() {
restSync.reset();
}
/**
* Synchronization control For ResetCountDownLatch. Uses AQS state to represent count.
*/
private static final class RestSync extends AbstractQueuedSynchronizer {
private final int initCount;
RestSync(int count) {
if (count < 0) {
throw new IllegalArgumentException("count must be greater than or equal to 0");
}
this.initCount = count;
setState(count);
}
protected void reset() {
setState(initCount);
}
int getCount() {
return getState();
}
@Override
protected int tryAcquireShared(int acquires) {
return (getState() == 0) ? 1 : -1;
}
@Override
protected boolean tryReleaseShared(int releases) {
for (;;) {
int count = getState();
if (count == 0) {
return false;
}
int nextCount = count - 1;
if (compareAndSetState(count, nextCount)) {
return nextCount == 0;
}
}
}
}
}