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

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

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

ReentrantReadWriteLock.getReadHoldCount介绍

[英]Queries the number of reentrant read holds on this lock by the current thread. A reader thread has a hold on a lock for each lock action that is not matched by an unlock action.
[中]查询当前线程对此锁的可重入读取保留数。读卡器线程为每个锁操作(与解锁操作不匹配)保留锁。

代码示例

代码示例来源:origin: Alluxio/alluxio

/**
 * Queries the number of reentrant read holds on this lock by the current thread. A reader thread
 * has a hold on a lock for each lock action that is not matched by an unlock action.
 *
 * @return the number of holds on the read lock by the current thread, or zero if the read lock is
 *         not held by the current thread
 */
public int getReadHoldCount() {
 return mDelegate.getReadHoldCount();
}

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

/**
 * The snapshot operation processing as following: <br>
 * 1. Create a Snapshot Handler, and do some initialization; <br>
 * 2. Put the handler into snapshotHandlers <br>
 * So when we consider if any snapshot is taking, we should consider both the takingSnapshotLock
 * and snapshotHandlers;
 * @return true to indicate that there're some running snapshots.
 */
public synchronized boolean isTakingAnySnapshot() {
 return this.takingSnapshotLock.getReadHoldCount() > 0 || this.snapshotHandlers.size() > 0;
}

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

public synchronized List<Proposal> getCommittedLog() {
  ReadLock rl = logLock.readLock();
  // only make a copy if this thread isn't already holding a lock
  if(logLock.getReadHoldCount() <=0) {
    try {
      rl.lock();
      return new LinkedList<Proposal>(this.committedLog);
    } finally {
      rl.unlock();
    }
  }
  return this.committedLog;
}

代码示例来源:origin: Alluxio/alluxio

@VisibleForTesting
boolean inodeReadLockedByCurrentThread(long inodeId) {
 return mInodeLocks.getRawReadWriteLock(inodeId).getReadHoldCount() > 0;
}

代码示例来源:origin: Alluxio/alluxio

@VisibleForTesting
boolean edgeReadLockedByCurrentThread(Edge edge) {
 return mEdgeLocks.getRawReadWriteLock(edge).getReadHoldCount() > 0;
}

代码示例来源:origin: org.apache.zookeeper/zookeeper

public synchronized LinkedList<Proposal> getCommittedLog() {
  ReadLock rl = logLock.readLock();
  // only make a copy if this thread isn't already holding a lock
  if(logLock.getReadHoldCount() <=0) {
    try {
      rl.lock();
      return new LinkedList<Proposal>(this.committedLog);
    } finally {
      rl.unlock();
    }
  } 
  return this.committedLog;
}

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

/**
 * Queries the number of reentrant read holds on this lock by the
 * current thread.  A reader thread has a hold on a lock for
 * each lock action that is not matched by an unlock action.
 *
 * @return the number of holds on the read lock by the current thread,
 *         or zero if the read lock is not held by the current thread
 */
public int getReadHoldCount() {
  return locks[curIdx()].getReadHoldCount();
}

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

/** {@inheritDoc} */
@Override public boolean holdsLock() {
  return lock.isWriteLockedByCurrentThread() || lock.getReadHoldCount() > 0;
}

代码示例来源:origin: yahoo/squidb

/**
 * Acquires an exclusive lock on the database. This is semantically similar to acquiring a write lock in a {@link
 * java.util.concurrent.locks.ReadWriteLock ReadWriteLock} but it is not generally necessary for protecting actual
 * database writes--it's only necessary when exclusive use of the database connection is required (e.g. while the
 * database is attached to another database).
 * <p>
 * Only one thread can hold an exclusive lock at a time. Calling this while on a thread that already holds a non-
 * exclusive lock is an error! We will throw an exception if this method is called while the
 * calling thread is in a transaction or otherwise holds the non-exclusive lock. Otherwise, this method will block
 * until all non-exclusive locks acquired with {@link #acquireNonExclusiveLock()} have been released, but will
 * prevent any new non-exclusive locks from being acquired while it blocks.
 */
protected void acquireExclusiveLock() {
  if (readWriteLock.getReadHoldCount() > 0 && readWriteLock.getWriteHoldCount() == 0) {
    throw new IllegalStateException("Can't acquire an exclusive lock when the calling thread is in a "
        + "transaction or otherwise holds a non-exclusive lock and not the exclusive lock");
  }
  readWriteLock.writeLock().lock();
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
  * Starts timing for the instrumented read lock.
  * It records the time to ThreadLocal.
  */
 @Override
 protected void startLockTiming() {
  if (readWriteLock.getReadHoldCount() == 1) {
   readLockHeldTimeStamp.set(getTimer().monotonicNow());
  }
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

@Override
public void unlock() {
 boolean needReport = readWriteLock.getReadHoldCount() == 1;
 long localLockReleaseTime = getTimer().monotonicNow();
 long localLockAcquireTime = readLockHeldTimeStamp.get();
 getLock().unlock();
 if (needReport) {
  readLockHeldTimeStamp.remove();
  check(localLockAcquireTime, localLockReleaseTime);
 }
}

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

Channel channelToClose = null;
Map<Integer, Callback<List<ByteBuffer>>> requestsToCancel = null;
boolean stateReadLockHeld = stateLock.getReadHoldCount() != 0;

代码示例来源:origin: pentaho/pentaho-kettle

int holdCount = inputRowSetsLock.getReadHoldCount();
for ( int i = 0; i < holdCount; i++ ) {
 inputRowSetsLock.readLock().unlock();

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

if (checkpointLock.getReadHoldCount() > 1 || safeToUpdatePageMemories())
  break;
else {

代码示例来源:origin: Alluxio/alluxio

/**
  * Tests {@link LockResource} with {@link ReentrantReadWriteLock}.
  */
 @Test
 public void reentrantReadWriteLock() {
  ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

  try (LockResource r1 = new LockResource(lock.readLock())) {
   try (LockResource r2 = new LockResource(lock.readLock())) {
    assertEquals(lock.getReadHoldCount(), 2);
    assertTrue(lock.readLock().tryLock());
    lock.readLock().unlock();
   }
  }
  assertEquals(lock.getReadHoldCount(), 0);

  try (LockResource r1 = new LockResource(lock.writeLock())) {
   try (LockResource r2 = new LockResource(lock.readLock())) {
    assertTrue(lock.isWriteLockedByCurrentThread());
    assertEquals(lock.getReadHoldCount(), 1);
   }
  }
  assertFalse(lock.isWriteLockedByCurrentThread());
  assertEquals(lock.getReadHoldCount(), 0);

  try (LockResource r = new LockResource(lock.readLock())) {
   assertFalse(lock.writeLock().tryLock());
  }
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-hdfs

public int getReadHoldCount() {
 return this.dirLock.getReadHoldCount();
}

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

@Test
  public void testBasics() {
    AutoReadWriteLock lock = new AutoReadWriteLock(new ReentrantReadWriteLock());
    try (AutoLock al = lock.lockForRead()) {
      Assert.assertTrue(lock.innerLock().getReadHoldCount() == 1);
    }
    Assert.assertTrue(lock.innerLock().getReadHoldCount() == 0);
    
    try (AutoLock al = lock.lockForWrite()) {
      Assert.assertTrue(lock.innerLock().getWriteHoldCount() == 1);
    }
    Assert.assertTrue(lock.innerLock().getWriteHoldCount() == 0);
  }
}

代码示例来源:origin: org.apache.hadoop/hadoop-hdfs

public void readLock() {
 coarseLock.readLock().lock();
 if (coarseLock.getReadHoldCount() == 1) {
  readLockHeldTimeStampNanos.set(timer.monotonicNowNanos());
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-hdfs

boolean hasReadLock() {
 return this.dirLock.getReadHoldCount() > 0 || hasWriteLock();
}

代码示例来源:origin: org.apache.hadoop/hadoop-hdfs

public void readLockInterruptibly() throws InterruptedException {
 coarseLock.readLock().lockInterruptibly();
 if (coarseLock.getReadHoldCount() == 1) {
  readLockHeldTimeStampNanos.set(timer.monotonicNowNanos());
 }
}

相关文章