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

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

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

ReentrantLock.isHeldByCurrentThread介绍

[英]Queries if this lock is held by the current thread.

Analogous to the Thread#holdsLock method for built-in monitor locks, this method is typically used for debugging and testing. For example, a method that should only be called while a lock is held can assert that this is the case:

class X }}

It can also be used to ensure that a reentrant lock is used in a non-reentrant manner, for example:

class X finally  
lock.unlock(); 
} 
} 
}}

[中]

代码示例

代码示例来源:origin: prestodb/presto

public synchronized void unlock()
{
  checkState(lock.isHeldByCurrentThread(), "Current thread does not hold lock");
  currentOwner = null;
  lock.unlock();
}

代码示例来源:origin: prestodb/presto

private synchronized void setOwner()
{
  checkState(lock.isHeldByCurrentThread(), "Current thread does not hold lock");
  currentOwner = Thread.currentThread();
  // NOTE: We do not use interrupted stack information to know that another
  // thread has attempted to interrupt the driver, and interrupt this new lock
  // owner.  The interrupted stack information is for debugging purposes only.
  // In the case of interruption, the caller should (and does) have a separate
  // state to prevent further processing in the Driver.
}

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

/**
 * Releases the lock on the transceiver's channel if held by the calling thread.
 */
public void unlockChannel() {
 if (channelLock.isHeldByCurrentThread()) {
  channelLock.unlock();
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

private static void attemptThreadControl(long threadId, Runnable r){
 //(get lock)
 boolean tookLock = false;
 if(!control.isHeldByCurrentThread()){
  control.lock();
  tookLock = true;
 }
 //(perform action)
 attemptThreadControlThreadsafe(threadId);
 if(threadId == currentThread){
  r.run();
 } else {
  queueTask(threadId, r);
 }
 //(release lock)
 assert control.isHeldByCurrentThread();
 if(tookLock){
  control.unlock();
 }
}

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

/** Enters this monitor when the guard is satisfied. Blocks indefinitely. */
public void enterWhenUninterruptibly(Guard guard) {
 if (guard.monitor != this) {
  throw new IllegalMonitorStateException();
 }
 final ReentrantLock lock = this.lock;
 boolean signalBeforeWaiting = lock.isHeldByCurrentThread();
 lock.lock();
 boolean satisfied = false;
 try {
  if (!guard.isSatisfied()) {
   awaitUninterruptibly(guard, signalBeforeWaiting);
  }
  satisfied = true;
 } finally {
  if (!satisfied) {
   leave();
  }
 }
}

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

@Subscribe
public void handleConfigurationEvent(MaterializedConfiguration conf) {
 try {
  lifecycleLock.lockInterruptibly();
  stopAllComponents();
  startAllComponents(conf);
 } catch (InterruptedException e) {
  logger.info("Interrupted while trying to handle configuration event");
  return;
 } finally {
  // If interrupted while trying to lock, we don't own the lock, so must not attempt to unlock
  if (lifecycleLock.isHeldByCurrentThread()) {
   lifecycleLock.unlock();
  }
 }
}

代码示例来源:origin: wildfly/wildfly

producer_lock.lock();
  try {
    if(count == 0 || count-1 == 0) {
      count=0;
      consumer_lock.unlock();
      return;
    producer_lock.unlock();
if(consumer_lock.isHeldByCurrentThread())
  consumer_lock.unlock();

代码示例来源:origin: prestodb/presto

public boolean tryLock(long timeout, TimeUnit unit)
    throws InterruptedException
{
  checkState(!lock.isHeldByCurrentThread(), "Lock is not reentrant");
  boolean acquired = lock.tryLock(timeout, unit);
  if (acquired) {
    setOwner();
  }
  return acquired;
}

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

/**
 * Release the lock that is preventing operations on this disk store during the backup process.
 */
public void releaseBackupLock() {
 ReentrantLock backupLock = getBackupLock();
 if (backupLock.isHeldByCurrentThread()) {
  backupLock.unlock();
 }
}

代码示例来源:origin: prestodb/presto

/** Enters this monitor when the guard is satisfied. Blocks indefinitely. */
public void enterWhenUninterruptibly(Guard guard) {
 if (guard.monitor != this) {
  throw new IllegalMonitorStateException();
 }
 final ReentrantLock lock = this.lock;
 boolean signalBeforeWaiting = lock.isHeldByCurrentThread();
 lock.lock();
 boolean satisfied = false;
 try {
  if (!guard.isSatisfied()) {
   awaitUninterruptibly(guard, signalBeforeWaiting);
  }
  satisfied = true;
 } finally {
  if (!satisfied) {
   leave();
  }
 }
}

代码示例来源:origin: io.prestosql/presto-main

public synchronized void unlock()
{
  checkState(lock.isHeldByCurrentThread(), "Current thread does not hold lock");
  currentOwner = null;
  lock.unlock();
}

代码示例来源:origin: DV8FromTheWorld/JDA

public static void locked(ReentrantLock lock, Runnable task)
{
  try
  {
    lock.lockInterruptibly();
    task.run();
  }
  catch (InterruptedException e)
  {
    throw new IllegalStateException(e);
  }
  finally
  {
    if (lock.isHeldByCurrentThread())
      lock.unlock();
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

assert !control.isHeldByCurrentThread();
    assert !control.isHeldByCurrentThread();
    backlog.poll().run();
 assert control.tryLock();
 assert ! threadsWaiting.isEmpty();
 control.lock();
 attemptThreadControlThreadsafe(-1);
 control.unlock();

代码示例来源:origin: prestodb/presto

public boolean tryLock()
{
  checkState(!lock.isHeldByCurrentThread(), "Lock is not reentrant");
  boolean acquired = lock.tryLock();
  if (acquired) {
    setOwner();
  }
  return acquired;
}

代码示例来源:origin: kiegroup/jbpm

protected void releaseAndCleanLock(Long id, RuntimeEngine runtime) {
  if (id != null) {
    ReentrantLock lock = engineLocks.get(id);
    if (lock != null) {
      if (!lock.hasQueuedThreads()) {
        logger.debug("Removing lock {} from list as non is waiting for it by {}", lock, runtime);
        engineLocks.remove(id);
      }
      if (lock.isHeldByCurrentThread()) {
        lock.unlock();
        logger.debug("{} unlocked by {}", lock, runtime);
      }
    }
  }
  
}

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

/**
 * Associate the transactional state with this thread.
 *
 * @param txState the transactional state.
 */
public void masqueradeAs(TXStateProxy txState) {
 assert txState != null;
 if (!txState.getLock().isHeldByCurrentThread()) {
  txState.getLock().lock();
 }
 setTXState(txState);
 if (logger.isDebugEnabled()) {
  logger.debug("masqueradeAs tx {}", txState);
 }
}

代码示例来源:origin: prestosql/presto

public synchronized void unlock()
{
  checkState(lock.isHeldByCurrentThread(), "Current thread does not hold lock");
  currentOwner = null;
  lock.unlock();
}

代码示例来源:origin: DV8FromTheWorld/JDA

public static <E> E locked(ReentrantLock lock, Supplier<E> task)
{
  try
  {
    lock.lockInterruptibly();
    return task.get();
  }
  catch (InterruptedException e)
  {
    throw new IllegalStateException(e);
  }
  finally
  {
    if (lock.isHeldByCurrentThread())
      lock.unlock();
  }
}

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

/**
 * Blocks until all partitioned steps have completed.  As each step completes
 * the PartitionAnalyzer analyzes the collector data received from each
 * partition (if there is any).
 *
 * @param tasks The {@link Future} that contains the reference to the executing step
 * @param result Set of completed {@link StepExecution}s
 * @throws Exception
 */
private void processPartitionResults(
    final List<Future<StepExecution>> tasks,
    final Set<StepExecution> result) throws Exception {
  while(true) {
    Thread.sleep(pollingInterval);
    try {
      lock.lock();
      while(!partitionDataQueue.isEmpty()) {
        analyzer.analyzeCollectorData(partitionDataQueue.remove());
      }
      processFinishedPartitions(tasks, result);
      if(tasks.size() == 0) {
        break;
      }
    } finally {
      if(lock.isHeldByCurrentThread()) {
        lock.unlock();
      }
    }
  }
}

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

@SuppressWarnings("unchecked")
public T get(final int index) {
 // Must be held by same thread
 Preconditions.checkState(locks.get(index).isHeldByCurrentThread());
 if (softReferenceList[index] != null) {
  return ((SoftReference<T>)(softReferenceList[index])).get();
 }
 return null;
}

相关文章