//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.util.thread;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

Reentrant lock that can be used in a try-with-resources statement.

Typical usage:

try (AutoLock lock = this.lock.lock())
{
    // Something
}
/** * <p>Reentrant lock that can be used in a try-with-resources statement.</p> * <p>Typical usage:</p> * <pre> * try (AutoLock lock = this.lock.lock()) * { * // Something * } * </pre> */
public class AutoLock implements AutoCloseable, Serializable { private static final long serialVersionUID = 3300696774541816341L; private final ReentrantLock _lock = new ReentrantLock();

Acquires the lock.

Returns:this AutoLock for unlocking
/** * <p>Acquires the lock.</p> * * @return this AutoLock for unlocking */
public AutoLock lock() { _lock.lock(); return this; }
See Also:
  • isHeldByCurrentThread.isHeldByCurrentThread()
Returns:whether this lock is held by the current thread
/** * @see ReentrantLock#isHeldByCurrentThread() * @return whether this lock is held by the current thread */
public boolean isHeldByCurrentThread() { return _lock.isHeldByCurrentThread(); }
Returns:a Condition associated with this lock
/** * @return a {@link Condition} associated with this lock */
public Condition newCondition() { return _lock.newCondition(); } // Package-private for testing only. boolean isLocked() { return _lock.isLocked(); } @Override public void close() { _lock.unlock(); }

A reentrant lock with a condition that can be used in a try-with-resources statement.

Typical usage:

// Waiting
try (AutoLock lock = _lock.lock())
{
    lock.await();
}
// Signaling
try (AutoLock lock = _lock.lock())
{
    lock.signalAll();
}
/** * <p>A reentrant lock with a condition that can be used in a try-with-resources statement.</p> * <p>Typical usage:</p> * <pre> * // Waiting * try (AutoLock lock = _lock.lock()) * { * lock.await(); * } * * // Signaling * try (AutoLock lock = _lock.lock()) * { * lock.signalAll(); * } * </pre> */
public static class WithCondition extends AutoLock { private final Condition _condition = newCondition(); @Override public AutoLock.WithCondition lock() { return (WithCondition)super.lock(); }
See Also:
  • signal.signal()
/** * @see Condition#signal() */
public void signal() { _condition.signal(); }
See Also:
  • signalAll.signalAll()
/** * @see Condition#signalAll() */
public void signalAll() { _condition.signalAll(); }
Throws:
See Also:
  • await.await()
/** * @see Condition#await() * @throws InterruptedException if the current thread is interrupted */
public void await() throws InterruptedException { _condition.await(); }
Params:
  • time – the time to wait
  • unit – the time unit
Throws:
See Also:
  • await.await(long, TimeUnit)
Returns:false if the waiting time elapsed
/** * @see Condition#await(long, TimeUnit) * @param time the time to wait * @param unit the time unit * @return false if the waiting time elapsed * @throws InterruptedException if the current thread is interrupted */
public boolean await(long time, TimeUnit unit) throws InterruptedException { return _condition.await(time, unit); } } }