Java Code Examples for java.util.concurrent.Semaphore#hasQueuedThreads()

The following examples show how to use java.util.concurrent.Semaphore#hasQueuedThreads() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OnHeapStoreEvictionTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 2
Source File: SemaphoreTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Spin-waits until s.hasQueuedThreads() becomes true.
 */
void waitForQueuedThreads(Semaphore s) {
    long startTime = System.nanoTime();
    while (!s.hasQueuedThreads()) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            throw new AssertionFailedError("timed out");
        Thread.yield();
    }
}
 
Example 3
Source File: SemaphoreTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testReleaseAcquireDifferentThreads(boolean fair,
                                               final AcquireMethod acquirer) {
    final Semaphore s = new Semaphore(0, fair);
    final int rounds = 4;
    long startTime = System.nanoTime();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            for (int i = 0; i < rounds; i++) {
                assertFalse(s.hasQueuedThreads());
                if (i % 2 == 0)
                    acquirer.acquire(s);
                else
                    acquirer.acquire(s, 3);
            }}});

    for (int i = 0; i < rounds; i++) {
        while (! (s.availablePermits() == 0 && s.hasQueuedThreads()))
            Thread.yield();
        assertTrue(t.isAlive());
        if (i % 2 == 0)
            s.release();
        else
            s.release(3);
    }
    awaitTermination(t);
    assertEquals(0, s.availablePermits());
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
 
Example 4
Source File: SemaphoreTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Spin-waits until s.hasQueuedThreads() becomes true.
 */
void waitForQueuedThreads(Semaphore s) {
    long startTime = System.nanoTime();
    while (!s.hasQueuedThreads()) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            throw new AssertionFailedError("timed out");
        Thread.yield();
    }
}
 
Example 5
Source File: SemaphoreTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testReleaseAcquireDifferentThreads(boolean fair,
                                               final AcquireMethod acquirer) {
    final Semaphore s = new Semaphore(0, fair);
    final int rounds = 4;
    long startTime = System.nanoTime();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            for (int i = 0; i < rounds; i++) {
                assertFalse(s.hasQueuedThreads());
                if (i % 2 == 0)
                    acquirer.acquire(s);
                else
                    acquirer.acquire(s, 3);
            }}});

    for (int i = 0; i < rounds; i++) {
        while (! (s.availablePermits() == 0 && s.hasQueuedThreads()))
            Thread.yield();
        assertTrue(t.isAlive());
        if (i % 2 == 0)
            s.release();
        else
            s.release(3);
    }
    awaitTermination(t);
    assertEquals(0, s.availablePermits());
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
 
Example 6
Source File: TestBoundedInMemoryQueue.java    From hudi with Apache License 2.0 4 votes vote down vote up
private boolean isQueueFull(Semaphore rateLimiter) {
  return (rateLimiter.availablePermits() == 0 && rateLimiter.hasQueuedThreads());
}
 
Example 7
Source File: TcpConnection.java    From esj with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean hasStarted() {
    Semaphore running = manager.getRunning();
    return (running != null) && (running.hasQueuedThreads() != false);
}