DynamicIterator extends AbstractIterator<T> { public void finish() { synchronized (queue) { finished = true; } available.release(); } DynamicIterator(int initialSize);  DynamicIterator(); boolean add(T element); void finish();  }
@Test(groups = "fast") public void testMultipleWriters() throws ExecutionException, InterruptedException, TimeoutException { ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).build()); Future<Set<Integer>> consumer = executor.submit(() -> Sets.newHashSet(iterator)); executor.invokeAll( asList(producer(iterator, 0, 1, 2, 3, 4), producer(iterator, 5, 6, 7, 8, 9)), 1, TimeUnit.SECONDS); iterator.finish(); Set<Integer> result = consumer.get(1, TimeUnit.SECONDS); assertEquals(result, Sets.newHashSet(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); }