java.util.concurrent.locks.ReentrantLock.unlock()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(5.9k)|赞(0)|评价(0)|浏览(289)

本文整理了Java中java.util.concurrent.locks.ReentrantLock.unlock方法的一些代码示例,展示了ReentrantLock.unlock的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ReentrantLock.unlock方法的具体详情如下:
包路径:java.util.concurrent.locks.ReentrantLock
类名称:ReentrantLock
方法名:unlock

ReentrantLock.unlock介绍

[英]Attempts to release this lock.

If the current thread is the holder of this lock then the hold count is decremented. If the hold count is now zero then the lock is released. If the current thread is not the holder of this lock then IllegalMonitorStateException is thrown.
[中]试图释放此锁。
如果当前线程是该锁的持有者,则持有者计数将减少。如果保持计数现在为零,则释放锁。如果当前线程不是此锁的持有者,则会引发IllegalMonitorStateException。

代码示例

代码示例来源:origin: google/guava

@Override
public boolean isCancelled() {
 lock.lock();
 try {
  return currentFuture.isCancelled();
 } finally {
  lock.unlock();
 }
}

代码示例来源:origin: alibaba/druid

public void setEnable(boolean enable) {
  lock.lock();
  try {
    this.enable = enable;
    if (!enable) {
      notEmpty.signalAll();
      notEmptySignalCount++;
    }
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

/**
 * Signals a waiting put. Called only from take/poll.
 */
private void signalNotFull() {
  final ReentrantLock putLock = this.putLock;
  putLock.lock();
  try {
    notFull.signal();
  } finally {
    putLock.unlock();
  }
}

代码示例来源:origin: alibaba/cobar

@Override
public void terminate() throws InterruptedException {
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    while (nodeCount > 0) {
      taskFinished.await();
    }
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: apache/incubator-druid

@Nullable
private T pollObject(long timeoutMs) throws InterruptedException
{
 long nanos = TIME_UNIT.toNanos(timeoutMs);
 final ReentrantLock lock = this.lock;
 lock.lockInterruptibly();
 try {
  while (objects.isEmpty()) {
   if (nanos <= 0) {
    return null;
   }
   nanos = notEnough.awaitNanos(nanos);
  }
  return objects.pop();
 }
 finally {
  lock.unlock();
 }
}

代码示例来源:origin: apache/incubator-druid

private T takeObject() throws InterruptedException
{
 final ReentrantLock lock = this.lock;
 lock.lockInterruptibly();
 try {
  while (objects.isEmpty()) {
   notEnough.await();
  }
  return objects.pop();
 }
 finally {
  lock.unlock();
 }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Invokes the supplied {@link Runnable} if the {@link Queue} lock was obtained without blocking.
 *
 * @param runnable the operation to perform.
 * @return {@code true} if the lock was available and the operation was performed.
 * @since 1.618
 */
protected boolean _tryWithLock(Runnable runnable) {
  if (lock.tryLock()) {
    try {
      runnable.run();
    } finally {
      lock.unlock();
    }
    return true;
  } else {
    return false;
  }
}

代码示例来源:origin: alibaba/canal

public Events<Event> get(Position start, int batchSize) throws InterruptedException, CanalStoreException {
  final ReentrantLock lock = this.lock;
  lock.lockInterruptibly();
  try {
    try {
      while (!checkUnGetSlotAt((LogPosition) start, batchSize))
        notEmpty.await();
    } catch (InterruptedException ie) {
      notEmpty.signal(); // propagate to non-interrupted thread
      throw ie;
    }
    return doGet(start, batchSize);
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: apache/hbase

@Override
public void put(E e) throws InterruptedException {
 if (e == null) throw new NullPointerException();
 lock.lock();
 try {
  while (queue.remainingCapacity() == 0) {
   notFull.await();
  }
  this.queue.add(e);
  notEmpty.signal();
 } finally {
  lock.unlock();
 }
}

代码示例来源:origin: apache/flink

@Override
public AsyncResult poll() throws InterruptedException {
  lock.lockInterruptibly();
  try {
    while (completedQueue.isEmpty()) {
      hasCompletedEntries.await();
    }
    numberEntries--;
    notFull.signalAll();
    LOG.debug("Polled element from unordered stream element queue. New filling degree " +
      "({}/{}).", numberEntries, capacity);
    return completedQueue.poll();
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: spring-projects/spring-framework

if (!propHolder.refreshLock.tryLock()) {
propHolder.refreshLock.lock();
propHolder.refreshLock.unlock();

代码示例来源:origin: apache/hbase

private void sendStopSignal() {
 if (lock.tryLock()) {
  try {
   waitCond.signalAll();
   syncCond.signalAll();
  } finally {
   lock.unlock();
  }
 }
}

代码示例来源:origin: alibaba/canal

public void destory() {
  try {
    lock.lockInterruptibly();
    timer.cancel();
    binlogs.clear();
    nextCondition.signalAll();// 唤醒线程,通知退出
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: google/guava

/**
 * Enters this monitor if the guard is satisfied. Blocks indefinitely acquiring the lock, but does
 * not wait for the guard to be satisfied, and may be interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 * @throws InterruptedException if interrupted while waiting
 */
public boolean enterIfInterruptibly(Guard guard) throws InterruptedException {
 if (guard.monitor != this) {
  throw new IllegalMonitorStateException();
 }
 final ReentrantLock lock = this.lock;
 lock.lockInterruptibly();
 boolean satisfied = false;
 try {
  return satisfied = guard.isSatisfied();
 } finally {
  if (!satisfied) {
   lock.unlock();
  }
 }
}

代码示例来源:origin: alibaba/cobar

@Override
public void terminate() throws InterruptedException {
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    while (nodeCount > 0) {
      taskFinished.await();
    }
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

/**
 * Signals a waiting take. Called only from put/offer (which do not
 * otherwise ordinarily lock takeLock.)
 */
private void signalNotEmpty() {
  final ReentrantLock takeLock = this.takeLock;
  takeLock.lock();
  try {
    notEmpty.signal();
  } finally {
    takeLock.unlock();
  }
}

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

public E pollFirst(long timeout, TimeUnit unit)
    throws InterruptedException {
  long nanos = unit.toNanos(timeout);
  final ReentrantLock lock = this.lock;
  lock.lockInterruptibly();
  try {
    E x;
    while ((x = unlinkFirst()) == null) {
      if (nanos <= 0)
        return null;
      nanos = notEmpty.awaitNanos(nanos);
    }
    return x;
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: google/guava

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
 // Ensure that a task cannot be rescheduled while a cancel is ongoing.
 lock.lock();
 try {
  return currentFuture.cancel(mayInterruptIfRunning);
 } finally {
  lock.unlock();
 }
}

代码示例来源:origin: apache/flink

@Override
public <T> void put(StreamElementQueueEntry<T> streamElementQueueEntry) throws InterruptedException {
  lock.lockInterruptibly();
  try {
    while (queue.size() >= capacity) {
      notFull.await();
    }
    addEntry(streamElementQueueEntry);
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: alipay/sofa-rpc

@Override
public void clear() {
  lock.tryLock();
  try {
    kvMap.clear();
    vkMap.clear();
  } finally {
    lock.unlock();
  }
}

相关文章