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

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

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

ReentrantLock.lock介绍

[英]Acquires the lock.

Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.

If the current thread already holds the lock then the hold count is incremented by one and the method returns immediately.

If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired, at which time the lock hold count is set to one.
[中]获得锁。
如果锁未被另一个线程持有,则获取锁并立即返回,将锁持有计数设置为1。
如果当前线程已经持有锁,那么持有计数将增加1,方法立即返回。
如果锁由另一个线程持有,那么出于线程调度目的,当前线程将被禁用,并处于休眠状态,直到获得锁,此时锁持有计数设置为1。

代码示例

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

@Override
public boolean isCancelled() {
 lock.lock();
 try {
  return currentFuture.isCancelled();
 } 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: alibaba/druid

public int getPoolingPeak() {
  lock.lock();
  try {
    return poolingPeak;
  } finally {
    lock.unlock();
  }
}

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

public int getNotEmptyWaitThreadPeak() {
  lock.lock();
  try {
    return notEmptyWaitThreadPeak;
  } finally {
    lock.unlock();
  }
}

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

public boolean isFull() {
  lock.lock();
  try {
    return this.poolingCount + this.activeCount >= this.maxActive;
  } finally {
    lock.unlock();
  }
}

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

@Override
public int getPoolingCount() {
  lock.lock();
  try {
    return poolingCount;
  } finally {
    lock.unlock();
  }
}

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

public int getNotEmptyWaitThreadCount() {
  lock.lock();
  try {
    return notEmptyWaitThreadCount;
  } finally {
    lock.unlock();
  }
}

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

public long getConnectCount() {
  lock.lock();
  try {
    return connectCount;
  } finally {
    lock.unlock();
  }
}

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

public int getActiveCount() {
  lock.lock();
  try {
    return activeCount;
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: stackoverflow.com

private ReentrantLock lock;

public void foo() {
 ...
 lock.lock();
 ...
}

public void bar() {
 ...
 lock.unlock();
 ...
}

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

/**
 * Get the unallocated memory (not in the free list or in use)
 */
public long unallocatedMemory() {
  lock.lock();
  try {
    return this.nonPooledAvailableMemory;
  } 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: google/guava

/**
 * Returns an estimate of the number of threads waiting for the given guard to become satisfied.
 * Note that because timeouts and interrupts may occur at any time, the estimate serves only as an
 * upper bound on the actual number of waiters. This method is designed for use in monitoring of
 * the system state, not for synchronization control.
 */
public int getWaitQueueLength(Guard guard) {
 if (guard.monitor != this) {
  throw new IllegalMonitorStateException();
 }
 lock.lock();
 try {
  return guard.waiterCount;
 } finally {
  lock.unlock();
 }
}

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

public Set<DruidPooledConnection> getActiveConnections() {
  activeConnectionLock.lock();
  try {
    return new HashSet<DruidPooledConnection>(this.activeConnections.keySet());
  } finally {
    activeConnectionLock.unlock();
  }
}

代码示例来源:origin: greenrobot/greenDAO

@Override
public void remove(Iterable< K> keys) {
  lock.lock();
  try {
    for (K key : keys) {
      map.remove(key);
    }
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: greenrobot/greenDAO

@Override
public void remove(K key) {
  lock.lock();
  try {
    map.remove(key);
  } finally {
    lock.unlock();
  }
}

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

/**
 * The number of threads blocked waiting on memory
 */
public int queued() {
  lock.lock();
  try {
    return this.waiters.size();
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: greenrobot/greenDAO

@Override
public void put(K key, T entity) {
  lock.lock();
  try {
    map.put(key, new WeakReference<T>(entity));
  } finally {
    lock.unlock();
  }
}

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

public void testExplicitOrdering_noViolations() {
 lock1.lock();
 lock3.lock();
 lock3.unlock();
 lock2.lock();
 lock3.lock();
}

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

public int getWaitThreadCount() {
  lock.lock();
  try {
    return lock.getWaitQueueLength(notEmpty);
  } finally {
    lock.unlock();
  }
}

相关文章