Java Code Examples for java.util.concurrent.LinkedBlockingDeque#put()

The following examples show how to use java.util.concurrent.LinkedBlockingDeque#put() . 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: LinkedBlockingDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * drainTo empties full deque, unblocking a waiting put.
 */
public void testDrainToWithActivePut() throws InterruptedException {
    final LinkedBlockingDeque q = populatedDeque(SIZE);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(new Integer(SIZE + 1));
        }});

    t.start();
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertTrue(l.size() >= SIZE);
    for (int i = 0; i < SIZE; ++i)
        assertEquals(l.get(i), new Integer(i));
    t.join();
    assertTrue(q.size() + l.size() >= SIZE);
}
 
Example 2
Source File: LinkedBlockingDequeTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * drainTo empties full deque, unblocking a waiting put.
 */
public void testDrainToWithActivePut() throws InterruptedException {
    final LinkedBlockingDeque q = populatedDeque(SIZE);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(new Integer(SIZE + 1));
        }});

    t.start();
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertTrue(l.size() >= SIZE);
    for (int i = 0; i < SIZE; ++i)
        assertEquals(l.get(i), new Integer(i));
    t.join();
    assertTrue(q.size() + l.size() >= SIZE);
}
 
Example 3
Source File: LinkedBlockingDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * all elements successfully put are contained
 */
public void testPut() throws InterruptedException {
    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        Integer x = new Integer(i);
        q.put(x);
        assertTrue(q.contains(x));
    }
    assertEquals(0, q.remainingCapacity());
}
 
Example 4
Source File: TestCodecPool.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 1000)
public void testMultiThreadedDecompressorPool() throws InterruptedException {
  final int iterations = 4;
  ExecutorService threadpool = Executors.newFixedThreadPool(3);
  final LinkedBlockingDeque<Decompressor> queue = new LinkedBlockingDeque<Decompressor>(
      2 * iterations);

  Callable<Boolean> consumer = new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
      Decompressor dc = queue.take();
      CodecPool.returnDecompressor(dc);
      return dc != null;
    }
  };

  Callable<Boolean> producer = new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
      Decompressor c = CodecPool.getDecompressor(codec);
      queue.put(c);
      return c != null;
    }
  };

  for (int i = 0; i < iterations; i++) {
    threadpool.submit(consumer);
    threadpool.submit(producer);
  }

  // wait for completion
  threadpool.shutdown();
  threadpool.awaitTermination(1000, TimeUnit.SECONDS);

  assertEquals(LEASE_COUNT_ERR, 0,
      CodecPool.getLeasedDecompressorsCount(codec));
}
 
Example 5
Source File: TestCodecPool.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 1000)
public void testMultiThreadedDecompressorPool() throws InterruptedException {
  final int iterations = 4;
  ExecutorService threadpool = Executors.newFixedThreadPool(3);
  final LinkedBlockingDeque<Decompressor> queue = new LinkedBlockingDeque<Decompressor>(
      2 * iterations);

  Callable<Boolean> consumer = new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
      Decompressor dc = queue.take();
      CodecPool.returnDecompressor(dc);
      return dc != null;
    }
  };

  Callable<Boolean> producer = new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
      Decompressor c = CodecPool.getDecompressor(codec);
      queue.put(c);
      return c != null;
    }
  };

  for (int i = 0; i < iterations; i++) {
    threadpool.submit(consumer);
    threadpool.submit(producer);
  }

  // wait for completion
  threadpool.shutdown();
  threadpool.awaitTermination(1000, TimeUnit.SECONDS);

  assertEquals(LEASE_COUNT_ERR, 0,
      CodecPool.getLeasedDecompressorsCount(codec));
}
 
Example 6
Source File: LinkedBlockingDequeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * all elements successfully put are contained
 */
public void testPut() throws InterruptedException {
    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        Integer x = new Integer(i);
        q.put(x);
        assertTrue(q.contains(x));
    }
    assertEquals(0, q.remainingCapacity());
}
 
Example 7
Source File: GoogleWebmasterExtractorIterator.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private void onSuccess(ProducerJob job, List<String[]> results, LinkedBlockingDeque<String[]> responseQueue,
    ConcurrentLinkedDeque<ProducerJob> pagesToRetry) {
  int size = results.size();
  if (size == GoogleWebmasterClient.API_ROW_LIMIT) {
    List<? extends ProducerJob> granularJobs = job.partitionJobs();
    if (granularJobs.isEmpty()) {
      //The job is not divisible
      //TODO: 99.99% cases we are good. But what if it happens, what can we do?
      log.warn(String.format(
          "There might be more query data for your job %s. Currently, downloading more than the Google API limit '%d' is not supported.",
          job, GoogleWebmasterClient.API_ROW_LIMIT));
    } else {
      log.info(String.format("Partition current job %s", job));
      pagesToRetry.addAll(granularJobs);
      return;
    }
  }

  log.debug(String.format("Finished %s. Current Queue size: %d. Record size: %d.", job, responseQueue.size(), size));
  try {
    for (String[] r : results) {
      responseQueue.put(r);
    }
  } catch (InterruptedException e) {
    log.error(e.getMessage());
    throw new RuntimeException(e);
  }
}