Prefab for actual resource lock
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
package mc.core.events.v3.lock;
|
||||||
|
|
||||||
|
import mc.core.events.v3.runner.ExecutorThread;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
public class CustomReentrantLock extends ReentrantLock {
|
||||||
|
private void checkThread() {
|
||||||
|
if (!(Thread.currentThread() instanceof ExecutorThread))
|
||||||
|
throw new RuntimeException("Unable to obtain this resource outside Async Executor");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void lock() {
|
||||||
|
checkThread();
|
||||||
|
super.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void lockInterruptibly() throws InterruptedException {
|
||||||
|
checkThread();
|
||||||
|
super.lockInterruptibly();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryLock() {
|
||||||
|
checkThread();
|
||||||
|
return super.tryLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
|
||||||
|
checkThread();
|
||||||
|
return super.tryLock(timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unlock() {
|
||||||
|
checkThread();
|
||||||
|
super.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user