BlockingObservableIterator(int batchSize) { this.queue = new SpscLinkedArrayQueue<T>(batchSize); this.lock = new ReentrantLock(); this.condition = lock.newCondition(); }
public void clear() { Lock l = new ReentrantLock(); l.lock(); try { // do nothing } finally { l.unlock(); } } }
public void setUseUnfairLock(boolean useUnfairLock) { if (lock.isFair() == !useUnfairLock) { return; } if (!this.inited) { final ReentrantLock lock = this.lock; lock.lock(); try { if (!this.inited) { this.lock = new ReentrantLock(!useUnfairLock); this.notEmpty = this.lock.newCondition(); this.empty = this.lock.newCondition(); this.useUnfairLock = useUnfairLock; } } finally { lock.unlock(); } } }
private Lock lock = new ReentrantLock(); private Condition notFull = lock.newCondition(); private Condition notEmpty = lock.newCondition(); lock.lock(); try { while(queue.size() == capacity) { notEmpty.signal(); } finally { lock.unlock(); lock.lock(); try { while(queue.isEmpty()) { return item; } finally { lock.unlock();
Lock lock = new ReentrantLock() if (lock.tryLock()) { try { // do stuff using the lock... } finally { lock.unlock(); } } ...
protected void createLockOnNewProcessInstance(Long id, RuntimeEngine runtime) { if (!isUseLocking()) { logger.debug("Locking on runtime manager disabled"); return; } ReentrantLock newLock = new ReentrantLock(); ReentrantLock lock = engineLocks.putIfAbsent(id, newLock); if (lock == null) { lock = newLock; } lock.lock(); logger.debug("[on new process instance] Lock {} created and stored in list by {}", lock, runtime); }
String threadName = Thread.currentThread().getName(); try { first.lock(); latch.countDown(); System.out.println(threadName + ": locked first lock"); latch.await(); System.out.println(threadName + ": attempting to lock second lock"); second.lock(); System.out.println(threadName + ": never reached"); } catch (InterruptedException e) { Lock lock1 = new ReentrantLock(), lock2 = new ReentrantLock(); new Thread(new Locker(latch, lock1, lock2), "Thread 1").start(); new Thread(new Locker(latch, lock2, lock1), "Thread 2").start();
private final ReentrantLock lock = new ReentrantLock(); private final Condition tryAgain = lock.newCondition(); private volatile boolean finished = false; lock.unlock();
private void unlock(String requestId, String name, long start) { LOG.trace("{} - Unlocking {} ({})", name, requestId, JavaUtils.duration(start)); ReentrantLock lock = requestLocks.computeIfAbsent(requestId, (r) -> new ReentrantLock()); lock.unlock(); }
/** * Retrieves partition {@code data} correspondent to specified partition index if it exists in local storage or * loads it using the specified {@code supplier}. Unlike {@link ConcurrentMap#computeIfAbsent(Object, Function)}, * this method guarantees that function will be called only once. * * @param <D> Type of data. * @param part Partition index. * @param supplier Partition {@code data} supplier. * @return Partition {@code data}. */ @SuppressWarnings("unchecked") <D> D computeDataIfAbsent(int part, Supplier<D> supplier) { Object data = storage.get(part); if (data == null) { Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock()); lock.lock(); try { data = storage.computeIfAbsent(part, p -> supplier.get()); } finally { lock.unlock(); } } return (D)data; } }
public void setMaxWait(long maxWaitMillis) { if (maxWaitMillis == this.maxWait) { return; } if (maxWaitMillis > 0 && useUnfairLock == null && !this.inited) { final ReentrantLock lock = this.lock; lock.lock(); try { if ((!this.inited) && (!lock.isFair())) { this.lock = new ReentrantLock(true); this.notEmpty = this.lock.newCondition(); this.empty = this.lock.newCondition(); } } finally { lock.unlock(); } } if (inited) { LOG.error("maxWait changed : " + this.maxWait + " -> " + maxWaitMillis); } this.maxWait = maxWaitMillis; }
/** * {@inheritDoc} * * This method creates an async call to the server, and blocks until the server * has finished annotating the object. */ @Override public void annotate(Annotation annotation) { final Lock lock = new ReentrantLock(); final Condition annotationDone = lock.newCondition(); annotate(Collections.singleton(annotation), 1, (Annotation annInput) -> { try { lock.lock(); annotationDone.signal(); } finally { lock.unlock(); } }); try { lock.lock(); annotationDone.await(); // Only wait for one callback to complete; only annotating one document } catch (InterruptedException e) { log.info("Interrupt while waiting for annotation to return"); } finally { lock.unlock(); } }
Lock lock = new ReentrantLock(); ...... if (lock.tryLock()) { // Got the lock try { // Process record } finally { // Make sure to unlock so that we don't cause a deadlock lock.unlock(); } } else { // Someone else had the lock, abort }
protected void createLockOnGetEngine(Long id, RuntimeEngine runtime) { if (!isUseLocking()) { logger.debug("Locking on runtime manager disabled"); return; } if (id != null) { ReentrantLock newLock = new ReentrantLock(); ReentrantLock lock = engineLocks.putIfAbsent(id, newLock); if (lock == null) { lock = newLock; logger.debug("New lock created as it did not exist before"); } else { logger.debug("Lock exists with {} waiting threads", lock.getQueueLength()); } logger.debug("Trying to get a lock {} for {} by {}", lock, id, runtime); lock.lock(); logger.debug("Lock {} taken for {} by {} for waiting threads by {}", lock, id, runtime, lock.hasQueuedThreads()); } }
BlockingFlowableIterator(int batchSize) { this.queue = new SpscArrayQueue<T>(batchSize); this.batchSize = batchSize; this.limit = batchSize - (batchSize >> 2); this.lock = new ReentrantLock(); this.condition = lock.newCondition(); }
private void createColumnFamily(final String name) throws RocksDBException { COLUMN_FAMILY_LOCKS.putIfAbsent(name, new ReentrantLock()); final Lock l = COLUMN_FAMILY_LOCKS.get(name); l.lock(); try { if(!COLUMN_FAMILIES.containsKey(name)) { final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions().optimizeLevelStyleCompaction(); final ColumnFamilyHandle cfHandle = rocksDb.createColumnFamily( new ColumnFamilyDescriptor(name.getBytes(UTF_8), cfOptions) ); COLUMN_FAMILIES.put(name, new ColumnFamily(cfHandle, cfOptions)); } } finally { l.unlock(); } }
ReentrantLock objectLock = null; lock.lock(); try { value = (T) registry.getIfPresent(key); objectLock = locks.get(key); } else { objectLock = new ReentrantLock(); locks.put(key, objectLock); lock.unlock(); objectLock.lock(); try{ lock.lock(); try { value = (T) registry.getIfPresent(key); lock.unlock(); locks.remove(key); } finally { lock.unlock();
private final Lock lock = new ReentrantLock(); private final Condition isEmpty = lock.newCondition(); private final Condition isFull = lock.newCondition(); lock.lock(); try { while(!usedData) {//wait for data to be used lock.unlock();//interrupt or not, release lock lock.lock(); try { while(usedData) {//usedData is lingo for empty lock.unlock();
Lock lock = this.locks.get(stringKey); if (lock == null) { this.locks.putIfAbsent(stringKey, new ReentrantLock()); lock = this.locks.get(stringKey); lock.unlock();
public void writeLock(final Object key) { ReentrantLock lock; synchronized (locks) { lock = (ReentrantLock) locks.get(key); if (lock == null) { lock = new ReentrantLock(); locks.put(key, lock); } } // Must be outside the above synchronized section, since this call may block. lock.lock(); }