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

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

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

ReentrantLock.getHoldCount介绍

[英]Queries the number of holds on this lock by the current thread.

A thread has a hold on a lock for each lock action that is not matched by an unlock action.

The hold count information is typically only used for testing and debugging purposes. For example, if a certain section of code should not be entered with the lock already held then we can assert that fact:

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

[中]查询当前线程对此锁的保留次数。
一个线程对每个锁操作都有一个保持锁,而锁操作与解锁操作不匹配。
保持计数信息通常仅用于测试和调试目的。例如,如果某段代码不应在锁定状态下输入,则我们可以断言这一事实:

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

代码示例

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

/**
 * Returns the number of times the current thread has entered this monitor in excess of the number
 * of times it has left. Returns 0 if the current thread is not occupying this monitor.
 */
public int getOccupiedDepth() {
 return lock.getHoldCount();
}

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

/**
 * Returns the number of times the current thread has entered this monitor in excess of the number
 * of times it has left. Returns 0 if the current thread is not occupying this monitor.
 */
public int getOccupiedDepth() {
 return lock.getHoldCount();
}

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

/**
 * Returns the number of times the current thread has entered this monitor in excess of the number
 * of times it has left. Returns 0 if the current thread is not occupying this monitor.
 */
public int getOccupiedDepth() {
 return lock.getHoldCount();
}

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

/** Leaves this monitor. May be called only by a thread currently occupying this monitor. */
public void leave() {
 final ReentrantLock lock = this.lock;
 try {
  // No need to signal if we will still be holding the lock when we return
  if (lock.getHoldCount() == 1) {
   signalNextWaiter();
  }
 } finally {
  lock.unlock(); // Will throw IllegalMonitorStateException if not held
 }
}

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

/** Leaves this monitor. May be called only by a thread currently occupying this monitor. */
public void leave() {
 final ReentrantLock lock = this.lock;
 try {
  // No need to signal if we will still be holding the lock when we return
  if (lock.getHoldCount() == 1) {
   signalNextWaiter();
  }
 } finally {
  lock.unlock(); // Will throw IllegalMonitorStateException if not held
 }
}

代码示例来源:origin: Atmosphere/atmosphere

/**
 * Release current lock and signal waiters if any
 */
private void releaseLock() {
  try {
    if (lock.getHoldCount() == 1) {
      signalWaiter();
    }
  } finally {
    lock.unlock();
  }
}

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

/** Leaves this monitor. May be called only by a thread currently occupying this monitor. */
public void leave() {
 final ReentrantLock lock = this.lock;
 try {
  // No need to signal if we will still be holding the lock when we return
  if (lock.getHoldCount() == 1) {
   signalNextWaiter();
  }
 } finally {
  lock.unlock(); // Will throw IllegalMonitorStateException if not held
 }
}

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

boolean holdsLock(final Object key) {
  synchronized (locks) {
    final ReentrantLock lock = (ReentrantLock) locks.get(key);
    if (lock != null) {
      return lock.getHoldCount() != 0;
    }
  }
  return false;
}
/** Stores a value */

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

boolean holdsLock(final Object key) {
  synchronized (locks) {
    final ReentrantLock lock = (ReentrantLock) locks.get(key);
    if (lock != null) {
      return lock.getHoldCount() != 0;
    }
  }
  return false;
}

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

public void writeUnLock(final Object key) {
  synchronized (locks) {
    final ReentrantLock lock = (ReentrantLock) locks.get(key);
    if (lock == null) {
      throw new IllegalMonitorStateException("Cannot unlock prior to locking");
    }
    if (lock.getHoldCount() == 0) {
      throw new IllegalMonitorStateException("Cannot unlock prior to locking");
    }
    lock.unlock();
    if (lock.getHoldCount() == 0) {
      locks.remove(key);
    }
  }
}

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

public void writeUnLock(final Object key) {
    synchronized (locks) {
      final ReentrantLock lock = (ReentrantLock) locks.get(key);
      if (lock == null) {
        throw new IllegalMonitorStateException("Cannot unlock prior to locking");
      }
      if (lock.getHoldCount() == 0) {
        throw new IllegalMonitorStateException("Cannot unlock prior to locking");
      }
      lock.unlock();
    }
  }
}

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

public void writeUnLock(final Object key) {
  synchronized (locks) {
    final ReentrantLock lock = (ReentrantLock) locks.get(key);
    if (lock == null) {
      throw new IllegalMonitorStateException("Cannot unlock prior to locking");
    }
    if (lock.getHoldCount() == 0) {
      throw new IllegalMonitorStateException("Cannot unlock prior to locking");
    }
    lock.unlock();
    // TODO: stop lock from being removed when another worker is trying
    // to acquire it
    // TODO: review w/ J2SE 5.0
    // if (lock.holds() == 0) {
    // locks.remove(key);
    // }
  }
}

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

@Override
public void unlock() {
  if (!this.delegate.isHeldByCurrentThread()) {
    throw new IllegalMonitorStateException("You do not own mutex at " + this.path);
  }
  if (this.delegate.getHoldCount() > 1) {
    this.delegate.unlock();
    return;
  }
  try {
    this.mutex.delete(this.path);
  }
  catch (Exception e) {
    throw new DataAccessResourceFailureException("Failed to release mutex at " + this.path, e);
  }
  finally {
    this.delegate.unlock();
  }
}

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

throw new IllegalStateException("You do not own lock at " + this.lockKey);
if (this.localLock.getHoldCount() > 1) {
  this.localLock.unlock();
  return;

代码示例来源:origin: org.apache.ratis/ratis-proto-shaded

/**
 * Returns the number of times the current thread has entered this monitor in excess of the number
 * of times it has left. Returns 0 if the current thread is not occupying this monitor.
 */
public int getOccupiedDepth() {
 return lock.getHoldCount();
}

代码示例来源:origin: com.atlassian.guava/guava

/**
 * Returns the number of times the current thread has entered this monitor in excess of the number
 * of times it has left. Returns 0 if the current thread is not occupying this monitor.
 */
public int getOccupiedDepth() {
 return lock.getHoldCount();
}

代码示例来源:origin: org.testifyproject.external/external-guava

/**
 * Returns the number of times the current thread has entered this monitor in excess of the number
 * of times it has left. Returns 0 if the current thread is not occupying this monitor.
 */
public int getOccupiedDepth() {
 return lock.getHoldCount();
}

代码示例来源:origin: io.prestosql.cassandra/cassandra-driver

/**
 * Returns the number of times the current thread has entered this monitor in excess of the number
 * of times it has left. Returns 0 if the current thread is not occupying this monitor.
 */
public int getOccupiedDepth() {
 return lock.getHoldCount();
}

代码示例来源:origin: com.vaadin/vaadin-server

if (((ReentrantLock) getLockInstance()).getHoldCount() == 1) {
  ultimateRelease = true;
  getService().runPendingAccessTasks(this);

代码示例来源:origin: org.shoal/shoal-gms-impl

private void lockLog(String method) {
    if (LOG.isLoggable(Level.FINE)){
      LOG.log(Level.FINE, MessageFormat.format("{0} viewLock Hold count :{1}, lock queue count:{2}", method, viewLock.getHoldCount(), viewLock.getQueueLength()));
    }
  }
}

相关文章