com.github.sviperll.concurrent.TimeoutingLock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chicory-core Show documentation
Show all versions of chicory-core Show documentation
Basic utilities to compensate shortcomings of Java SE.
Part of chicory.
/*
* Copyright (c) 2013, Victor Nazarov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Victor Nazarov nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.github.sviperll.concurrent;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
/**
* Timeouting lock is a decorator for some other Lock implementation.
* TimeoutingLock is constructor with maximum wait time as a parameter.
* TimeoutingLock#lock operation throws RuntimeTimeoutException when
* lock can't be aquired in less then maximum wait time.
*
* TimeoutingLocks can be used when blocking is anacceptable.
* Applications can use TimeoutingLocks and inform user about
* resource being blocked instead of blocking.
*/
public class TimeoutingLock implements Lock {
private final Lock lock;
private final long time;
private final TimeUnit unit;
public TimeoutingLock(Lock lock, long time, TimeUnit unit) {
this.lock = lock;
this.time = time;
this.unit = unit;
}
@Override
public void lock() throws RuntimeTimeoutException {
for (;;) {
try {
lockInterruptibly();
return;
} catch (InterruptedException ex) {
}
}
}
@Override
public void lockInterruptibly() throws RuntimeTimeoutException, InterruptedException {
try {
boolean success = lock.tryLock(time, unit);
if (!success)
throw new TimeoutException("Lock timeouted");
} catch (TimeoutException ex) {
throw new RuntimeTimeoutException("Lock timouting in TimeoutingLock", ex);
}
}
@Override
public boolean tryLock() {
return lock.tryLock();
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
return lock.tryLock(time, unit);
}
@Override
public void unlock() throws RuntimeTimeoutException {
lock.unlock();
}
@Override
public Condition newCondition() {
return lock.newCondition();
}
}