use of java8.util.function.IntFunction in project streamsupport by stefan-zobel.
the class ConcurrentAssociateTest method testOnce.
private static void testOnce(final String desc, final BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
final ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<Object, Object>();
final CountDownLatch s = new CountDownLatch(1);
final Supplier<Runnable> putter = new Supplier<Runnable>() {
@Override
public Runnable get() {
return new Runnable() {
@Override
public void run() {
try {
s.await();
} catch (InterruptedException e) {
}
for (int i = 0; i < N; i++) {
Object o = new X();
associator.accept(m, o);
if (!m.containsKey(o)) {
throw new AssociationFailure(desc + " failed: entry does not exist");
}
}
}
};
}
};
Stream<CompletableFuture<Void>> putters = IntStreams.range(0, availableProcessors).mapToObj(new IntFunction<Runnable>() {
public Runnable apply(int i) {
return putter.get();
}
}).map(new Function<Runnable, CompletableFuture<Void>>() {
@Override
public CompletableFuture<Void> apply(Runnable runnable) {
return CompletableFuture.runAsync(runnable);
}
});
CompletableFuture<Void> all = CompletableFuture.allOf(putters.toArray(new IntFunction<CompletableFuture<Void>[]>() {
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<Void>[] apply(int size) {
return (CompletableFuture<Void>[]) new CompletableFuture[size];
}
}));
// Trigger the runners to start
s.countDown();
try {
all.join();
} catch (CompletionException e) {
Throwable t = e.getCause();
if (t instanceof AssociationFailure) {
throw (AssociationFailure) t;
} else {
throw e;
}
}
}
Aggregations