Lock tryLock - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Lock tryLock method.

This code example shows how to use the following methods:lock, tryLock, unlock
    public SynchronizedToolingImplementationLoader(ToolingImplementationLoader delegate) {
        this.delegate = delegate;
    } 
 
    public ConsumerConnection create(Distribution distribution, ProgressLoggerFactory progressLoggerFactory, ConnectionParameters connectionParameters, BuildCancellationToken cancellationToken) {
        if (lock.tryLock()) {
            try { 
                return delegate.create(distribution, progressLoggerFactory, connectionParameters, cancellationToken);
            } finally { 
                lock.unlock();
            } 
        } 
        ProgressLogger logger = progressLoggerFactory.newOperation(SynchronizedToolingImplementationLoader.class);
        logger.setDescription("Wait for the other thread to finish acquiring the distribution");
        logger.started();
        lock.lock();
        try { 
            return delegate.create(distribution, progressLoggerFactory, connectionParameters, cancellationToken);
        } finally { 
            lock.unlock();

9
CodeRank
This code example shows how to use the following methods:tryLock
	 * Attempts to lock the store 
	 * 
	 * @return true on success 
	 */ 
	public boolean tryLock() { 
		return resizeLock.tryLock();
	} 
 
	/** 
	 * Gets if the store is uniform 
	 */ 
	public boolean isUniform() { 
		return store.get() instanceof AtomicShortIntUniformBackingArray;
	} 
} 
7
CodeRank
This code example shows how to use the following methods:tryLock, unlock
        return find(ETERNAL_KEY, key, loader);
    } 
    private static <V> V find(String cacheName, String key, Callable<V> loader) throws Exception {
        Lock lock = getLock(key);
        try { 
            if(!lock.tryLock(30, TimeUnit.SECONDS)) {
                throw new IllegalStateException("Timed out trying to get the log for the JeevesCache");
            } 
            JeevesJCS cache = JeevesJCS.getInstance(cacheName);
             
            @SuppressWarnings("unchecked") 
            V value = (V) cache.get(key);
             
            if (value == null) {
                value = loader.call();
                cache.put(key, value);
            } 
            return value;
        } finally { 
            lock.unlock();

7
CodeRank
This code example shows how to use the following methods:lock, tryLock, unlock
        String session = context.getSessionImpl().toString();
 
        // Acquire the exclusive lock for accessing session internals. 
        // No other session should be holding the lock, so we log a 
        // message to let the user know of such cases. 
        if (!lock.tryLock()) {
            if (isWriteOperation
                    && operation instanceof SessionWriteOperation) {
                Exception trace = new Exception(
                        "Stack trace of concurrent access to " + session);
                log.warn("Attempt to perform " + operation
                        + " while another thread is concurrently writing" 
                        + " to " + session + ". Blocking until the other"
                        + " thread is finished using this session. Please" 
                        + " review your code to avoid concurrent use of" 
                        + " a session.", trace);
            } else if (log.isDebugEnabled()) { 
                Exception trace = new Exception(
                        "Stack trace of concurrent access to " + session);
                log.debug("Attempt to perform " + operation + " while"

7
CodeRank
This code example shows how to use the following methods:tryLock, unlock
    public VerifiedSingleKahluaThread(PrintStream stream, Platform platform, KahluaTable environment) {
        super(stream, platform, environment);
    } 
 
    private void lock() { 
        if (!lock.tryLock()) {
            throw new IllegalStateException("Multiple threads may not access the same lua thread");
        } 
    } 
 
    private void unlock() { 
        lock.unlock();
    } 
 
    @Override 
    public int call(int arguments) {
        lock(); 
        try { 
            return super.call(arguments);
        } finally { 
6
CodeRank
This code example shows how to use the following methods:tryLock, unlock
	public LockTryLock() { 
		super("ReentrantLock.tryLock"); 
	} 
 
	public void execute() { 
		boolean locked = lock.tryLock();
		if (locked) {
			try { 
				value = 0;
			} finally { 
				lock.unlock();
			} 
		} 
	} 
 
	public int getValue() { 
		return value;
	} 
 
	public void setValue(int value) {
6
CodeRank
This code example shows how to use the following methods:lock, tryLock, unlock
 
    public void testSimple() { 
        @SuppressWarnings("deprecation") 
        Lock l = new SimpleLock();
 
        assertTrue(l.tryLock());
        assertFalse(l.tryLock());
        l.unlock();
        l.lock();
        l.unlock();
    } 
 
} 
6
CodeRank
This code example shows how to use the following methods:lock, tryLock, unlock
    public SynchronizedToolingImplementationLoader(ToolingImplementationLoader delegate) {
        this.delegate = delegate;
    } 
 
    public ConsumerConnection create(Distribution distribution, ProgressLoggerFactory progressLoggerFactory, boolean verboseLogging) {
        if (lock.tryLock()) {
            try { 
                return delegate.create(distribution, progressLoggerFactory, verboseLogging);
            } finally { 
                lock.unlock();
            } 
        } 
        ProgressLogger logger = progressLoggerFactory.newOperation(SynchronizedToolingImplementationLoader.class);
        logger.setDescription("Wait for the other thread to finish acquiring the distribution");
        logger.started();
        lock.lock();
        try { 
            return delegate.create(distribution, progressLoggerFactory, verboseLogging);
        } finally { 
            lock.unlock();

6
CodeRank
This code example shows how to use the following methods:lock, tryLock, unlock
  public static void main(String args[]) throws InterruptedException, KeeperException {
    ZooQueueLock lock = new ZooQueueLock("/lock", true);
    DistributedReadWriteLock rlocker = new DistributedReadWriteLock(lock, "reader".getBytes(UTF_8));
    DistributedReadWriteLock wlocker = new DistributedReadWriteLock(lock, "wlocker".getBytes(UTF_8));
    final Lock readLock = rlocker.readLock();
    readLock.lock();
    final Lock readLock2 = rlocker.readLock();
    readLock2.lock();
    final Lock writeLock = wlocker.writeLock();
    if (writeLock.tryLock(100, TimeUnit.MILLISECONDS))
      throw new RuntimeException("Write lock achieved during read lock!");
    readLock.unlock();
    readLock2.unlock();
    writeLock.lock();
    if (readLock.tryLock(100, TimeUnit.MILLISECONDS))
      throw new RuntimeException("Read lock achieved during write lock!");
    final Lock writeLock2 = DistributedReadWriteLock.recoverLock(lock, "wlocker".getBytes(UTF_8));
    writeLock2.unlock();
    readLock.lock();
    System.out.println("success");

6
CodeRank
This code example shows how to use the following methods:tryLock, unlock
 
    @edu.umd.cs.findbugs.annotations.SuppressWarnings("VO_VOLATILE_INCREMENT") 
    private void addNewWorkerIfRequired() { 
        if (size < maxPoolSize) {
            try { 
                if (lock.tryLock(TIME, TimeUnit.MILLISECONDS)) {
                    try { 
                        if (size < maxPoolSize && getQueueSize() > 0) {
                            size++;
                            cachedExecutor.execute(new Worker());
                        } 
                    } finally { 
                        lock.unlock();
                    } 
                } 
            } catch (InterruptedException ignored) {
                EmptyStatement.ignore(ignored);
            } 
        } 
    } 
6
CodeRank

Related Lock tryLock Questions & Answers:

tryLock Method Overview
Acquires the lock only if it is free at the time of invocation.
See Code Examples for other Java 8 Lock Methods: