use of java.util.concurrent.ForkJoinPool in project ddf by codice.
the class ValidateExecutor method execute.
public static List<ValidateReport> execute(List<Metacard> metacards, List<MetacardValidator> validators) throws ExecutionException, InterruptedException {
ForkJoinPool forkJoinPool = new ForkJoinPool();
List<ValidateReport> results;
results = forkJoinPool.submit(() -> metacards.stream().parallel().map(metacard -> generateReport(metacard, validators)).collect(Collectors.toList())).get();
return results;
}
use of java.util.concurrent.ForkJoinPool in project quasar by puniverse.
the class FiberForkJoinScheduler method createForkJoinPool.
private ForkJoinPool createForkJoinPool(String name, int parallelism, UncaughtExceptionHandler exceptionHandler, MonitorType monitorType) {
final MonitoredForkJoinPool pool = new MonitoredForkJoinPool(name, parallelism, new ExtendedForkJoinWorkerFactory(name) {
@Override
protected ExtendedForkJoinWorkerThread createThread(ForkJoinPool pool) {
return new FiberWorkerThread(pool);
}
}, exceptionHandler, true);
pool.setMonitor(createForkJoinPoolMonitor(name, pool, monitorType));
return pool;
}
use of java.util.concurrent.ForkJoinPool in project metron by apache.
the class ReaderSpliteratorTest method getNumberOfBatches.
private int getNumberOfBatches(final ReaderSpliterator spliterator) throws ExecutionException, InterruptedException {
final AtomicInteger numSplits = new AtomicInteger(0);
// we want to wrap the spliterator and count the (valid) splits
Spliterator<String> delegatingSpliterator = spy(spliterator);
doAnswer(invocationOnMock -> {
Spliterator<String> ret = spliterator.trySplit();
if (ret != null) {
numSplits.incrementAndGet();
}
return ret;
}).when(delegatingSpliterator).trySplit();
Stream<String> stream = StreamSupport.stream(delegatingSpliterator, true);
// now run it in a parallel pool and do some calculation that doesn't really matter.
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
forkJoinPool.submit(() -> {
Map<String, Integer> threads = stream.parallel().map(s -> Thread.currentThread().getName()).collect(Collectors.toMap(s -> s, s -> 1, Integer::sum));
Assert.assertTrue(threads.size() > 0);
}).get();
return numSplits.get();
}
use of java.util.concurrent.ForkJoinPool in project metron by apache.
the class HashFunctionsTest method tlsh_multithread.
@Test
public void tlsh_multithread() throws Exception {
// we want to ensure that everything is threadsafe, so we'll spin up some random data
// generate some hashes and then do it all in parallel and make sure it all matches.
Map<Map.Entry<byte[], Map<String, Object>>, String> hashes = new HashMap<>();
Random r = new Random(0);
for (int i = 0; i < 20; ++i) {
byte[] d = new byte[256];
r.nextBytes(d);
Map<String, Object> config = new HashMap<String, Object>() {
{
put(TLSHHasher.Config.BUCKET_SIZE.key, r.nextBoolean() ? 128 : 256);
put(TLSHHasher.Config.CHECKSUM.key, r.nextBoolean() ? 1 : 3);
}
};
String hash = (String) run("HASH(data, 'tlsh', config)", ImmutableMap.of("config", config, "data", d));
Assert.assertNotNull(hash);
hashes.put(new AbstractMap.SimpleEntry<>(d, config), hash);
}
ForkJoinPool forkJoinPool = new ForkJoinPool(5);
forkJoinPool.submit(() -> hashes.entrySet().parallelStream().forEach(kv -> {
Map<String, Object> config = kv.getKey().getValue();
byte[] data = kv.getKey().getKey();
String hash = (String) run("HASH(data, 'tlsh', config)", ImmutableMap.of("config", config, "data", data));
Assert.assertEquals(hash, kv.getValue());
}));
}
Aggregations