java.util.concurrent.Semaphore.hasQueuedThreads()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(103)

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

Semaphore.hasQueuedThreads介绍

[英]Queries whether any threads are waiting to acquire. Note that because cancellations may occur at any time, a truereturn does not guarantee that any other thread will ever acquire. This method is designed primarily for use in monitoring of the system state.
[中]查询是否有线程正在等待获取。请注意,因为取消可能随时发生,所以truereturn并不保证任何其他线程都会获得。该方法主要用于监控系统状态。

代码示例

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

private Future<Void> makeWorkStuckAtSemaphore( int delta )
{
  semaphore.drainPermits();
  Future<Void> concurrentWork = executor.submit( new CallableWork( new AddWork( delta ) ) );
  assertThrows( TimeoutException.class, () -> concurrentWork.get( 10, TimeUnit.MILLISECONDS ) );
  while ( !semaphore.hasQueuedThreads() )
  {
    usleep( 1 );
  }
  // good, the concurrent AddWork is now stuck on the semaphore
  return concurrentWork;
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testFaultsDoNotGetToEvictionAdvisor() throws StoreAccessException {
 final Semaphore semaphore = new Semaphore(0);
 final OnHeapStoreForTests<String, String> store = newStore(SystemTimeSource.INSTANCE, noAdvice());
 ExecutorService executor = Executors.newCachedThreadPool();
 try {
  executor.submit(() -> store.getOrComputeIfAbsent("prime", key -> {
   semaphore.acquireUninterruptibly();
   return new OnHeapValueHolder<String>(0, 0, false) {
    @Override
    public String get() {
     return key;
    }
   };
  }));
  while (!semaphore.hasQueuedThreads());
  store.put("boom", "boom");
 } finally {
  semaphore.release(1);
  executor.shutdown();
 }
}

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

@Test
public void testTableProcedureDeadLockAfterRestarting() throws Exception {
 // let the shared procedure run first, but let it have a greater procId so when loading it will
 // be loaded at last.
 long procId1 = procExec.submitProcedure(new TableSharedProcedureWithId());
 long procId2 = procExec.submitProcedure(new TableExclusiveProcedureWithId());
 procExec.startWorkers();
 UTIL.waitFor(10000,
  () -> ((TableSharedProcedure) procExec.getProcedure(procId1)).latch.hasQueuedThreads());
 ProcedureTestingUtility.restart(procExec);
 ((TableSharedProcedure) procExec.getProcedure(procId1)).latch.release();
 ((TableExclusiveProcedure) procExec.getProcedure(procId2)).latch.release();
 UTIL.waitFor(10000, () -> procExec.isFinished(procId1));
 UTIL.waitFor(10000, () -> procExec.isFinished(procId2));
}

代码示例来源:origin: org.apache.commons/commons-pool2

public boolean hasQueuedThreads() {
    return semaphore.hasQueuedThreads();
  }
}

代码示例来源:origin: org.apache.commons/commons-pool2

public boolean hasQueuedThreads() {
    return semaphore.hasQueuedThreads();
  }
}

代码示例来源:origin: org.apache.jackrabbit/oak-core

boolean isClosing(){
  return runPermit.hasQueuedThreads();
}

代码示例来源:origin: apache/jackrabbit-oak

boolean isClosing(){
  return runPermit.hasQueuedThreads();
}

代码示例来源:origin: org.apache.sling/org.apache.sling.testing.sling-mock-oak

boolean isClosing(){
  return runPermit.hasQueuedThreads();
}

代码示例来源:origin: com.github.emc-mongoose/mongoose-storage-driver

@Override
public final boolean isIdle() {
  if(concurrencyLevel > 0) {
    return !concurrencyThrottle.hasQueuedThreads() &&
      concurrencyThrottle.availablePermits() >= concurrencyLevel;
  } else {
    return concurrencyThrottle.availablePermits() == Integer.MAX_VALUE;
  }
}

代码示例来源:origin: org.apache.apex/malhar-library

public boolean isEmptyAndBlocked()
{
 return numLeft.get() == 0 && semaphore.availablePermits() == 0 && semaphore.hasQueuedThreads();
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public String fulfilled() throws Exception {
  if (!waitBeforeUnlocking.hasQueuedThreads()) {
    return "no thread queued";
  }
  return null;
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public String fulfilled() throws Exception {
  if (!waitBeforeLocking.hasQueuedThreads()) {
    return "no thread queued";
  }
  return null;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-kenai

/** Returns true when the queue is empty and all the permits were released.
 *
 * @return
 */
public boolean isDone() {
  return q.isEmpty() && ( !s.hasQueuedThreads() ) ;
}

代码示例来源:origin: omero/server

/**
   * This method is called by Spring during server shut-down.
   */
  public void destroy() {
    final Semaphore semaphoreCopy = SHARED_SEMAPHORE;
    if (semaphoreCopy != null) {
      SHARED_SEMAPHORE = null;
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        // does not matter
      }
      while (semaphoreCopy.hasQueuedThreads()) {
        semaphoreCopy.release();
        try {
          Thread.sleep(1);
        } catch (InterruptedException e) {
          // does not matter
        }
      }
    }
  }
}

代码示例来源:origin: uber/hudi

private boolean isQueueFull(Semaphore rateLimiter) {
  return (rateLimiter.availablePermits() == 0 && rateLimiter.hasQueuedThreads());
 }
}

代码示例来源:origin: org.apache.uima/uimaj-as-core

public void stop() {
   super.stop(true);  // shutdown now
   
   // enable blocked threads to finish // https://issues.apache.org/jira/browse/UIMA-3433
  if ( semaphore != null ) {
   while ( semaphore.hasQueuedThreads()) {
    semaphore.release(); // permit any blocked threads to clean up
    try {
     Thread.sleep(1); // allow other thread to become unqueued
    } catch (InterruptedException e) {
    }  
   }
//      semaphore.drainPermits();  
//      while ( semaphore.availablePermits() > 0) {
//          semaphore.release();
//        }
  }
    
  this.cleanUp();
    // dont kill jUnit tests
  if (isTopLevelComponent() &&  System.getProperty("dontKill") == null) {
    System.exit(0);
  }
  
 }

代码示例来源:origin: apache/jackrabbit-oak

@Test
public void cancel() throws Exception {
  // block gc call
  store.semaphore.acquireUninterruptibly();
  Future<VersionGCStats> stats = gc();
  boolean gcBlocked = false;
  for (int i = 0; i < 10; i ++) {
    if (store.semaphore.hasQueuedThreads()) {
      gcBlocked = true;
      break;
    }
    Thread.sleep(100);
  }
  assertTrue(gcBlocked);
  // now cancel the GC
  gc.cancel();
  store.semaphore.release();
  assertTrue(stats.get().canceled);
}

代码示例来源:origin: apache/jackrabbit-oak

@Test
public void failParallelGC() throws Exception {
  // block gc call
  store.semaphore.acquireUninterruptibly();
  Future<VersionGCStats> stats = gc();
  boolean gcBlocked = false;
  for (int i = 0; i < 10; i ++) {
    if (store.semaphore.hasQueuedThreads()) {
      gcBlocked = true;
      break;
    }
    Thread.sleep(100);
  }
  assertTrue(gcBlocked);
  // now try to trigger another GC
  try {
    gc.gc(30, TimeUnit.MINUTES);
    fail("must throw an IOException");
  } catch (IOException e) {
    assertTrue(e.getMessage().contains("already running"));
  } finally {
    store.semaphore.release();
    stats.get();
  }
}

代码示例来源:origin: apache/jackrabbit-oak

@Test
public void asyncIndexerReindexAndPropertyIndexes() throws Exception{
  defnb.async("async", "nrt");
  defnb.indexRule("nt:base").property("foo").sync();
  addIndex(indexPath, defnb);
  root.commit();
  createPath("/a").setProperty("foo", "bar");
  root.commit();
  Semaphore s = new Semaphore(0);
  delayingEditorProvider.semaphore = s;
  AtomicReference<Throwable> th = new AtomicReference<>();
  Thread t = new Thread(this::runAsyncIndex);
  t.setUncaughtExceptionHandler((t1, e) -> th.set(e));
  t.start();
  while (!s.hasQueuedThreads()) {
    Thread.yield();
  }
  createPath("/b").setProperty("foo", "bar");
  root.commit();
  s.release(2);
  t.join();
  if (th.get() != null) {
    throw new AssertionError(th.get());
  }
}

代码示例来源:origin: org.apache.hbase/hbase-server

@Test
public void testTableProcedureDeadLockAfterRestarting() throws Exception {
 // let the shared procedure run first, but let it have a greater procId so when loading it will
 // be loaded at last.
 long procId1 = procExec.submitProcedure(new TableSharedProcedureWithId());
 long procId2 = procExec.submitProcedure(new TableExclusiveProcedureWithId());
 procExec.startWorkers();
 UTIL.waitFor(10000,
  () -> ((TableSharedProcedure) procExec.getProcedure(procId1)).latch.hasQueuedThreads());
 ProcedureTestingUtility.restart(procExec);
 ((TableSharedProcedure) procExec.getProcedure(procId1)).latch.release();
 ((TableExclusiveProcedure) procExec.getProcedure(procId2)).latch.release();
 UTIL.waitFor(10000, () -> procExec.isFinished(procId1));
 UTIL.waitFor(10000, () -> procExec.isFinished(procId2));
}

相关文章