下面列出了java.util.concurrent.locks.ReentrantLock#isLocked() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Wait until lock of specified workspace will be locked.
*
* @param workspaceId workspace id to which loch belongs
* @return true if lock is locked, false if lock doesn't exist
*/
private boolean waitUntilWorkspaceLockLockedWithQueueLength(String workspaceId) {
ReentrantLock lock;
try {
Field locks = DockerEnvironmentBackupManager.class.getDeclaredField("workspacesBackupLocks");
locks.setAccessible(true);
@SuppressWarnings("unchecked") // field workspacesBackupLocks newer change its type
ConcurrentHashMap<String, ReentrantLock> workspacesBackupLocks =
(ConcurrentHashMap<String, ReentrantLock>) locks.get(backupManager);
lock = workspacesBackupLocks.get(workspaceId);
if (lock != null) {
while (!lock.isLocked() || lock.getQueueLength() != 1) {
sleep(10);
}
return true;
}
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.error(e.getLocalizedMessage(), e);
} catch (InterruptedException ignore) {
// ok, exit
}
return false;
}
@Test
void runHooksShouldExecuteHooksSequentially() {
ReentrantLock reentrantLock = new ReentrantLock();
Answer<Publisher<Void>> lockAndSleepAnswer = invocationOnMock -> {
reentrantLock.lock();
Thread.sleep(Duration.ofMillis(100).toMillis());
reentrantLock.unlock();
return Mono.empty();
};
Answer<Publisher<Void>> throwIfLockedAnswer = invocationOnMock -> {
if (reentrantLock.isLocked()) {
throw new RuntimeException("This task is running while the previous one is waiting");
}
return Mono.empty();
};
when(hook1.notifyDelete(any())).thenAnswer(lockAndSleepAnswer);
when(hook2.notifyDelete(any())).thenAnswer(throwIfLockedAnswer);
assertThatCode(() -> testee.runHooks(DELETE_OPERATION).block())
.describedAs("RunHook does not throw if hooks are executed in a sequential manner")
.doesNotThrowAnyException();
}
/**
* 解锁
* @param info 请求信息
*/
private void unlock(ExchangeInfo info) {
String key = this.getKey(info);
ReentrantLock lock = LOCKS.get(key);
if (null != lock && lock.isLocked()) {
lock.unlock();
}
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public void assertNoLocks() {
for(ReentrantLock l:locks){
if(l.isLocked())
throw new InternalError("Some node is still locked by current thread");
}
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
throws RemoteException {
String response = "Hello with lock == " + receivedLock.isLocked();
return response;
}
@Override
public void run() {
if (waitIfPaused()) return;
if (delayIfNeed()) return;
ReentrantLock loadFromUriLock = imageLoadingInfo.loadFromUriLock;
log(LOG_START_DISPLAY_IMAGE_TASK);
if (loadFromUriLock.isLocked()) {
log(LOG_WAITING_FOR_IMAGE_LOADED);
}
loadFromUriLock.lock();
Bitmap bmp;
try {
if (checkTaskIsNotActual()) return;
bmp = configuration.memoryCache.get(memoryCacheKey);
if (bmp == null) {
bmp = tryLoadBitmap();
if (imageAwareCollected) return; // listener callback already was fired
if (bmp == null) return; // listener callback already was fired
if (checkTaskIsNotActual() || checkTaskIsInterrupted()) return;
if (options.shouldPreProcess()) {
log(LOG_PREPROCESS_IMAGE);
bmp = options.getPreProcessor().process(bmp);
if (bmp == null) {
L.e(ERROR_PRE_PROCESSOR_NULL);
}
}
if (bmp != null && options.isCacheInMemory()) {
log(LOG_CACHE_IMAGE_IN_MEMORY);
configuration.memoryCache.put(memoryCacheKey, bmp);
}
} else {
loadedFrom = LoadedFrom.MEMORY_CACHE;
log(LOG_GET_IMAGE_FROM_MEMORY_CACHE_AFTER_WAITING);
}
if (bmp != null && options.shouldPostProcess()) {
log(LOG_POSTPROCESS_IMAGE);
bmp = options.getPostProcessor().process(bmp);
if (bmp == null) {
L.e(ERROR_POST_PROCESSOR_NULL, memoryCacheKey);
}
}
} finally {
loadFromUriLock.unlock();
}
if (checkTaskIsNotActual() || checkTaskIsInterrupted()) return;
DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(bmp, imageLoadingInfo, engine, loadedFrom);
displayBitmapTask.setLoggingEnabled(writeLogs);
if (options.isSyncLoading()) {
displayBitmapTask.run();
} else {
handler.post(displayBitmapTask);
}
}
public void unlock(ReentrantLock lock) {
if (lock != null && lock.isLocked()) {
lock.unlock();
}
}
@Override
public void run() {
if (waitIfPaused())
return;
if (delayIfNeed())
return;
ReentrantLock loadFromUriLock = imageLoadingInfo.loadFromUriLock;
log(LOG_START_DISPLAY_IMAGE_TASK);
if (loadFromUriLock.isLocked()) {
log(LOG_WAITING_FOR_IMAGE_LOADED);
}
loadFromUriLock.lock();
Bitmap bmp;
try {
if (checkTaskIsNotActual())
return;
bmp = configuration.memoryCache.get(memoryCacheKey);
if (bmp == null) {
bmp = tryLoadBitmap();
if (imageViewCollected)
return; // listener callback already was fired
if (bmp == null)
return; // listener callback already was fired
if (checkTaskIsNotActual() || checkTaskIsInterrupted())
return;
if (options.shouldPreProcess()) {
log(LOG_PREPROCESS_IMAGE);
bmp = options.getPreProcessor().process(bmp);
if (bmp == null) {
L.e(ERROR_PRE_PROCESSOR_NULL);
}
}
if (bmp != null && options.isCacheInMemory()) {
log(LOG_CACHE_IMAGE_IN_MEMORY);
configuration.memoryCache.put(memoryCacheKey, bmp);
}
} else {
loadedFrom = LoadedFrom.MEMORY_CACHE;
log(LOG_GET_IMAGE_FROM_MEMORY_CACHE_AFTER_WAITING);
}
if (bmp != null && options.shouldPostProcess()) {
log(LOG_POSTPROCESS_IMAGE);
bmp = options.getPostProcessor().process(bmp);
if (bmp == null) {
L.e(ERROR_POST_PROCESSOR_NULL, memoryCacheKey);
}
}
} finally {
loadFromUriLock.unlock();
}
if (checkTaskIsNotActual() || checkTaskIsInterrupted())
return;
DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(bmp, imageLoadingInfo, engine, loadedFrom);
displayBitmapTask.setLoggingEnabled(writeLogs);
handler.post(displayBitmapTask);
}