Search in sources :

Example 21 with ForkJoinPool

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;
}
Also used : ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 22 with ForkJoinPool

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;
}
Also used : ExtendedForkJoinWorkerFactory(co.paralleluniverse.concurrent.forkjoin.ExtendedForkJoinWorkerFactory) MonitoredForkJoinPool(co.paralleluniverse.concurrent.forkjoin.MonitoredForkJoinPool) ExtendedForkJoinWorkerThread(co.paralleluniverse.concurrent.forkjoin.ExtendedForkJoinWorkerThread) MonitoredForkJoinPool(co.paralleluniverse.concurrent.forkjoin.MonitoredForkJoinPool) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 23 with ForkJoinPool

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 24 with ForkJoinPool

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());
    }));
}
Also used : StellarProcessorUtils.run(org.apache.metron.stellar.common.utils.StellarProcessorUtils.run) java.util(java.util) ImmutableMap(com.google.common.collect.ImmutableMap) MessageDigest(java.security.MessageDigest) SerializationUtils(org.apache.commons.lang.SerializationUtils) Security(java.security.Security) Test(org.junit.Test) Hex(org.apache.commons.codec.binary.Hex) StringUtils(org.apache.commons.lang3.StringUtils) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) TLSHHasher(org.apache.metron.stellar.common.utils.hashing.tlsh.TLSHHasher) ImmutableList(com.google.common.collect.ImmutableList) ForkJoinPool(java.util.concurrent.ForkJoinPool) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Assert(org.junit.Assert) ImmutableMap(com.google.common.collect.ImmutableMap) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Aggregations

ForkJoinPool (java.util.concurrent.ForkJoinPool)24 Test (org.junit.Test)6 List (java.util.List)5 Map (java.util.Map)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Collectors (java.util.stream.Collectors)4 File (java.io.File)3 Serializable (java.io.Serializable)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 BuildTarget (com.facebook.buck.model.BuildTarget)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ForkJoinWorkerThreadFactory (java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory)2 ForkJoinWorkerThread (java.util.concurrent.ForkJoinWorkerThread)2