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

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

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

Semaphore.acquireUninterruptibly介绍

[英]Acquires a permit from this semaphore, blocking until one is available.

Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.

If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until some other thread invokes the #release method for this semaphore and the current thread is next to be assigned a permit.

If the current thread is Thread#interruptwhile waiting for a permit then it will continue to wait, but the time at which the thread is assigned a permit may change compared to the time it would have received the permit had no interruption occurred. When the thread does return from this method its interrupt status will be set.
[中]从该信号量获取许可证,直到有许可证可用为止。
获得许可证(如果有)并立即返回,将可用许可证数量减少一个。
如果没有可用的许可证,那么当前线程将出于线程调度目的被禁用,并处于休眠状态,直到其他线程调用此信号量的#release方法,并且当前线程下一个将被分配许可证。
如果当前线程在等待许可证时为线程#中断,那么它将继续等待,但与未发生中断时线程收到许可证的时间相比,分配许可证的时间可能会发生变化。当线程确实从此方法返回时,将设置其中断状态。

代码示例

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

@Override
public void lock() {
 mAvailable.acquireUninterruptibly(mPermits);
}

代码示例来源:origin: com.zaxxer/HikariCP

public void suspend()
{
 acquisitionSemaphore.acquireUninterruptibly(MAX_PERMITS);
}

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

public boolean acquire() {
  throttle.acquireUninterruptibly();
  // Don't start new requests if there is an exception
  if (exceptions.size() > 0) {
    latch.decrementAndGet();
    throttle.release();
    return false;
  }
  return true;
}

代码示例来源:origin: signalapp/Signal-Server

@Override
public void execute(Runnable task) {
 semaphore.acquireUninterruptibly();
 try {
  super.execute(task);
 } catch (Throwable t) {
  semaphore.release();
  throw new RuntimeException(t);
 }
}

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

public void lock() {
  semaphore.acquireUninterruptibly();
  if (log.isDebugEnabled()) {
    owner = Thread.currentThread().getName();
    position = Thread.currentThread().getStackTrace()[2].toString();
    log.debug("<<< Lock {} acquired at {}", this.hashCode(), position);
  }
}

代码示例来源:origin: davemorrissey/subsampling-scale-image-view

/**
 * Acquire a decoder. Blocks until one is available.
 */
private BitmapRegionDecoder acquire() {
  available.acquireUninterruptibly();
  return getNextAvailable();
}

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

@Override
public void acquireUninterruptibly() {
 if (incHoldCount()) {
  super.acquireUninterruptibly();
 }
}

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

@Override
public void acquireGIIPermitUninterruptibly() {
 this.parallelGIIs.acquireUninterruptibly();
 this.stats.incInitialImageRequestsInProgress(1);
}

代码示例来源:origin: com.zaxxer/HikariCP

public void acquire() throws SQLException
{
 if (acquisitionSemaphore.tryAcquire()) {
   return;
 }
 else if (Boolean.getBoolean("com.zaxxer.hikari.throwIfSuspended")) {
   throw new SQLTransientException("The pool is currently suspended and configured to throw exceptions upon acquisition");
 }
 acquisitionSemaphore.acquireUninterruptibly();
}

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

private void flush() {
  semaphore.acquireUninterruptibly(config.getMaxConcurrentRequests());
  semaphore.release(config.getMaxConcurrentRequests());
}

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

@Override
public void lock() {
 SessionState.get().getCompileLock().lock();
 globalCompileQuotas.acquireUninterruptibly();
}

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

/**
 * {@inheritDoc}
 */
@Override
public void changeConfig(final BulkheadConfig newConfig) {
  synchronized (configChangesLock) {
    int delta =  newConfig.getMaxConcurrentCalls() - config.getMaxConcurrentCalls();
    if (delta < 0) {
      semaphore.acquireUninterruptibly(-delta);
    } else if (delta > 0) {
      semaphore.release(delta);
    }
    config = newConfig;
  }
}

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

public void enqueueUpdate( DatabaseIndex<? extends IndexReader> index, IndexUpdater indexUpdater, IndexEntryUpdate<?> update )
{
  updateQueueLimit.acquireUninterruptibly();
  Runnable eventualUpdate = () ->
  {
    try
    {
      indexUpdater.process( update );
    }
    catch ( IndexEntryConflictException e )
    {
      markAsFailed( index, e );
    }
    finally
    {
      updateQueueLimit.release();
    }
  };
  try
  {
    scheduler.schedule( Group.INDEX_UPDATING, eventualUpdate );
  }
  catch ( Exception e )
  {
    updateQueueLimit.release(); // Avoid leaking permits if job scheduling fails.
    throw e;
  }
}

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

public void add( int delta )
  {
    semaphore.acquireUninterruptibly();
    sum.add( delta );
    count.increment();
  }
}

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

public void lock(HoldType type) {
 while (true) {
  LockTransition transition = invoke(LockMessaging.lock(type));
  if (transition.isAcquired()) {
   currentState = LockMessaging.lock(type);
   return;
  } else {
   wakeup.acquireUninterruptibly();
  }
 }
}

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

/**
 * Adds write future to the pending list and returns the size of the queue.
 * <p>
 * Note that separate counter for the queue size is needed because in case of concurrent
 * calls this method should return different values (when queue size is 0 and 2 concurrent calls
 * occur exactly one call will return 1)
 *
 * @param writeFut Write request to add.
 * @return Updated size of the queue.
 */
int offerFuture(SessionWriteRequest writeFut) {
  boolean msgThread = GridNioBackPressureControl.threadProcessingMessage();
  if (sem != null && !msgThread)
    sem.acquireUninterruptibly();
  writeFut.messageThread(msgThread);
  boolean res = queue.offer(writeFut);
  assert res : "Future was not added to queue";
  return queue.sizex();
}

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

@Test
public void mustNotStartRecurringTasksWherePriorExecutionHasNotYetFinished()
{
  Runnable runnable = () ->
  {
    counter.incrementAndGet();
    semaphore.acquireUninterruptibly();
  };
  scheduler.submit( Group.STORAGE_MAINTENANCE, runnable, 100, 100 );
  for ( int i = 0; i < 4; i++ )
  {
    scheduler.tick();
    clock.forward( 100, TimeUnit.NANOSECONDS );
  }
  semaphore.release( Integer.MAX_VALUE );
  pools.getThreadPool( Group.STORAGE_MAINTENANCE ).shutDown();
  assertThat( counter.get(), is( 1 ) );
}

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

semaphore.acquireUninterruptibly();
};
JobHandle handle = scheduler.submit( Group.STORAGE_MAINTENANCE, recurring, 100, 100 );

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

@Test
public void testThreadPoolsUsingDefaultPool() throws Exception {
 Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/thread-pools.xml"));
 final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
 cacheManager.init();
 try {
  Cache<String, String> cache = cacheManager.createCache("testThreadPools", newCacheConfigurationBuilder(String.class, String.class, heap(10))
      .add(new DefaultCacheLoaderWriterConfiguration(ThreadRememberingLoaderWriter.class))
      .add(newUnBatchedWriteBehindConfiguration())
      .build());
  cache.put("foo", "bar");
  ThreadRememberingLoaderWriter.USED.acquireUninterruptibly();
  assertThat(ThreadRememberingLoaderWriter.LAST_SEEN_THREAD.getName(), containsString("[big]"));
 } finally {
  cacheManager.close();
 }
}

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

@Test
public void testThreadPools() throws Exception {
 Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/thread-pools.xml"));
 final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
 cacheManager.init();
 try {
  Cache<String, String> cache = cacheManager.createCache("testThreadPools", newCacheConfigurationBuilder(String.class, String.class, heap(10))
      .add(new DefaultCacheLoaderWriterConfiguration(ThreadRememberingLoaderWriter.class))
      .add(newUnBatchedWriteBehindConfiguration().useThreadPool("small"))
      .build());
  cache.put("foo", "bar");
  ThreadRememberingLoaderWriter.USED.acquireUninterruptibly();
  assertThat(ThreadRememberingLoaderWriter.LAST_SEEN_THREAD.getName(), containsString("[small]"));
 } finally {
  cacheManager.close();
 }
}

相关文章