コード例 #1
0
  @Test
  public void testPollWait() throws InterruptedException, IOException {
    final FileBlockingQueue<String> queue = getFileBlockingQueue();

    final AtomicLong start = new AtomicLong(System.currentTimeMillis());
    String m = queue.poll(1000, TimeUnit.MILLISECONDS);
    final AtomicLong duration = new AtomicLong(System.currentTimeMillis() - start.get());
    assertTrue(duration.get() >= 1000 && duration.get() <= 2000);
    assertNull(m);

    ExecutorService e = Executors.newFixedThreadPool(1);
    e.execute(
        new Runnable() {
          @Override
          public void run() {
            start.set(System.currentTimeMillis());
            String m = null;
            try {
              m = queue.poll(5000, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e1) {
              throw new RuntimeException(e1);
            }
            assertNotNull(m);
            duration.set(System.currentTimeMillis() - start.get());
          }
        });

    Thread.sleep(1000);
    queue.offer("testString");

    assertTrue(duration.get() < 4000);
  }
コード例 #2
0
 private void createFile(FileBlockingQueue<String> queue, int count) throws IOException {
   for (int i = 0; i < count; ++i) {
     assertTrue(queue.offer("testString" + i));
   }
 }