Java Code Examples for java.util.concurrent.locks.ReentrantLock#isLocked()

The following examples show how to use java.util.concurrent.locks.ReentrantLock#isLocked() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockerEnvironmentBackupManagerTest.java    From codenvy with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: PreDeletionHooksTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 3
Source File: BitfinexExchange.java    From GOAi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 解锁
 * @param info 请求信息
 */
private void unlock(ExchangeInfo info) {
    String key = this.getKey(info);
    ReentrantLock lock = LOCKS.get(key);
    if (null != lock && lock.isLocked()) {
        lock.unlock();
    }
}
 
Example 4
Source File: HelloImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 5
Source File: HelloImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 6
Source File: HelloImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 7
Source File: HelloImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 8
Source File: HelloImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 9
Source File: HelloImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 10
Source File: HelloImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 11
Source File: Locks.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void assertNoLocks() {
    for(ReentrantLock l:locks){
        if(l.isLocked())
            throw new InternalError("Some node is still locked by current thread");
    }
}
 
Example 12
Source File: HelloImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 13
Source File: HelloImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String sayHelloWithReentrantLock(ReentrantLock receivedLock)
        throws RemoteException {

    String response = "Hello with lock == " + receivedLock.isLocked();
    return response;
}
 
Example 14
Source File: LoadAndDisplayImageTask.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
@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);
	}
}
 
Example 15
Source File: ReentrantLockHelper.java    From ymate-platform-v2 with Apache License 2.0 4 votes vote down vote up
public void unlock(ReentrantLock lock) {
    if (lock != null && lock.isLocked()) {
        lock.unlock();
    }
}
 
Example 16
Source File: LoadAndDisplayImageTask.java    From Roid-Library with Apache License 2.0 4 votes vote down vote up
@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);
}