Java Code Examples for java.util.concurrent.SynchronousQueue#take()

The following examples show how to use java.util.concurrent.SynchronousQueue#take() . 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: AccountChooser.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private String selectAccount(Account accounts[]) {
  final SynchronousQueue<String> queue = new SynchronousQueue<String>();
  SelectAccount select = new SelectAccount(accounts, queue);
  select.start();
  Log.i(LOG_TAG, "Select: waiting for user...");
  String account = null;
  try {
    account = queue.take();
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  Log.i(LOG_TAG, "Selected: " + account);
  return NO_ACCOUNT.equals(account) ? null : account;
}
 
Example 2
Source File: SynchronousQueueDemo.java    From LearningOfThinkInJava with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException{
    SynchronousQueue<String> queue=new SynchronousQueue<String>();
    queue.take();
}
 
Example 3
Source File: SynchronousQueueDemo.java    From LearningOfThinkInJava with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException{
    SynchronousQueue<String> queue=new SynchronousQueue<String>();
    queue.take();
}
 
Example 4
Source File: SemaphoreBasedRateLimiterImplTest.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
@Test
public void acquirePermissionAndMetrics() throws Exception {

    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    RateLimiterConfig configSpy = spy(config);
    SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", configSpy,
        scheduledExecutorService);
    RateLimiter.Metrics detailedMetrics = limit.getMetrics();

    SynchronousQueue<Object> synchronousQueue = new SynchronousQueue<>();
    Thread thread = new Thread(() -> {
        run(() -> {
            for (int i = 0; i < LIMIT; i++) {
                synchronousQueue.put(O);
                limit.acquirePermission();
            }
            limit.acquirePermission();
        });
    });
    thread.setDaemon(true);
    thread.start();

    for (int i = 0; i < LIMIT; i++) {
        synchronousQueue.take();
    }

    awaitImpatiently()
        .atMost(100, TimeUnit.MILLISECONDS)
        .until(detailedMetrics::getAvailablePermissions, equalTo(0));
    awaitImpatiently()
        .atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(TIMED_WAITING));
    then(detailedMetrics.getAvailablePermissions()).isEqualTo(0);

    limit.refreshLimit();
    awaitImpatiently()
        .atMost(100, TimeUnit.MILLISECONDS)
        .until(detailedMetrics::getAvailablePermissions, equalTo(1));
    awaitImpatiently()
        .atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(TERMINATED));
    then(detailedMetrics.getAvailablePermissions()).isEqualTo(1);

    limit.changeLimitForPeriod(3);
    limit.refreshLimit();
    then(detailedMetrics.getAvailablePermissions()).isEqualTo(3);
}
 
Example 5
Source File: PipelineTest.java    From postgres-async-driver with Apache License 2.0 4 votes vote down vote up
private Connection getConnection() throws InterruptedException {
    SynchronousQueue<Connection> connQueue = new SynchronousQueue<>();
    pool.getConnection()
            .thenAccept(connQueue::offer);
    return c = connQueue.take();
}