Search in sources :

Example 71 with ExecutorService

use of java.util.concurrent.ExecutorService in project hibernate-orm by hibernate.

the class ConcurrentWriteTest method testManyUsers.

// Ignoring the test as it's more of a stress-test: this should be enabled manually
@Ignore
@Test
public void testManyUsers() throws Throwable {
    try {
        // setup - create users
        for (int i = 0; i < USER_COUNT; i++) {
            Customer customer = createCustomer(0);
            getCustomerIDs().add(customer.getId());
        }
        assertEquals("failed to create enough Customers", USER_COUNT, getCustomerIDs().size());
        final ExecutorService executor = Executors.newFixedThreadPool(USER_COUNT);
        CyclicBarrier barrier = new CyclicBarrier(USER_COUNT + 1);
        List<Future<Void>> futures = new ArrayList<Future<Void>>(USER_COUNT);
        for (Integer customerId : getCustomerIDs()) {
            Future<Void> future = executor.submit(new UserRunner(customerId, barrier));
            futures.add(future);
            // rampup
            Thread.sleep(LAUNCH_INTERVAL_MILLIS);
        }
        // wait for all threads to finish
        barrier.await(2, TimeUnit.MINUTES);
        log.info("All threads finished, let's shutdown the executor and check whether any exceptions were reported");
        for (Future<Void> future : futures) {
            future.get();
        }
        executor.shutdown();
        log.info("All future gets checked");
    } catch (Throwable t) {
        log.error("Error running test", t);
        throw t;
    }
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) CyclicBarrier(java.util.concurrent.CyclicBarrier) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 72 with ExecutorService

use of java.util.concurrent.ExecutorService in project languagetool by languagetool-org.

the class MultiThreadedJLanguageTool method performCheck.

@Override
protected List<RuleMatch> performCheck(List<AnalyzedSentence> analyzedSentences, List<String> sentences, List<Rule> allRules, ParagraphHandling paraMode, AnnotatedText annotatedText, RuleMatchListener listener) throws IOException {
    int charCount = 0;
    int lineCount = 0;
    int columnCount = 1;
    List<RuleMatch> ruleMatches = new ArrayList<>();
    ExecutorService executorService = getExecutorService();
    try {
        List<Callable<List<RuleMatch>>> callables = createTextCheckCallables(paraMode, annotatedText, analyzedSentences, sentences, allRules, charCount, lineCount, columnCount, listener);
        List<Future<List<RuleMatch>>> futures = executorService.invokeAll(callables);
        for (Future<List<RuleMatch>> future : futures) {
            ruleMatches.addAll(future.get());
        }
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    return ruleMatches;
}
Also used : ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) RuleMatch(org.languagetool.rules.RuleMatch) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException)

Example 73 with ExecutorService

use of java.util.concurrent.ExecutorService in project jersey by jersey.

the class TransportFilter method initializeChannelGroup.

private void initializeChannelGroup() throws IOException {
    if (closeWaitTask != null) {
        closeWaitTask.cancel(true);
        closeWaitTask = null;
    }
    if (channelGroup == null) {
        ThreadFactory threadFactory = threadPoolConfig.getThreadFactory();
        if (threadFactory == null) {
            threadFactory = new TransportThreadFactory(threadPoolConfig);
        }
        ExecutorService executor;
        if (threadPoolConfig.getQueue() != null) {
            executor = new QueuingExecutor(threadPoolConfig.getCorePoolSize(), threadPoolConfig.getMaxPoolSize(), threadPoolConfig.getKeepAliveTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, threadPoolConfig.getQueue(), false, threadFactory);
        } else {
            int taskQueueLimit = threadPoolConfig.getQueueLimit();
            if (taskQueueLimit == -1) {
                taskQueueLimit = Integer.MAX_VALUE;
            }
            executor = new QueuingExecutor(threadPoolConfig.getCorePoolSize(), threadPoolConfig.getMaxPoolSize(), threadPoolConfig.getKeepAliveTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(taskQueueLimit), true, threadFactory);
        }
        // Thread pool is owned by the channel group and will be shut down when channel group is shut down
        channelGroup = AsynchronousChannelGroup.withCachedThreadPool(executor, threadPoolConfig.getCorePoolSize());
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Example 74 with ExecutorService

use of java.util.concurrent.ExecutorService in project mapdb by jankotek.

the class Collection8Test method testForEachConcurrentStressTest.

public void testForEachConcurrentStressTest() throws Throwable {
    if (!impl.isConcurrent())
        return;
    final Collection c = impl.emptyCollection();
    final long testDurationMillis = SHORT_DELAY_MS;
    final AtomicBoolean done = new AtomicBoolean(false);
    final Object elt = impl.makeElement(1);
    ExecutorService pool = Executors.newCachedThreadPool();
    Runnable checkElt = () -> {
        while (!done.get()) c.stream().forEach((x) -> {
            assertSame(x, elt);
        });
    };
    Runnable addRemove = () -> {
        while (!done.get()) {
            assertTrue(c.add(elt));
            assertTrue(c.remove(elt));
        }
    };
    Future<?> f1 = pool.submit(checkElt);
    Future<?> f2 = pool.submit(addRemove);
    Thread.sleep(testDurationMillis);
    done.set(true);
    pool.shutdown();
    assertTrue(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
    assertNull(f1.get(LONG_DELAY_MS, MILLISECONDS));
    assertNull(f2.get(LONG_DELAY_MS, MILLISECONDS));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection)

Example 75 with ExecutorService

use of java.util.concurrent.ExecutorService in project mapdb by jankotek.

the class LinkedBlockingDequeTest method testPollInExecutor.

/**
     * timed poll retrieves elements across Executor threads
     */
public void testPollInExecutor() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
    final CheckedBarrier threadsStarted = new CheckedBarrier(2);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    try (PoolCleaner cleaner = cleaner(executor)) {
        executor.execute(new CheckedRunnable() {

            public void realRun() throws InterruptedException {
                assertNull(q.poll());
                threadsStarted.await();
                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
                checkEmpty(q);
            }
        });
        executor.execute(new CheckedRunnable() {

            public void realRun() throws InterruptedException {
                threadsStarted.await();
                q.put(one);
            }
        });
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)4945 Test (org.junit.Test)1738 ArrayList (java.util.ArrayList)1223 Future (java.util.concurrent.Future)1121 CountDownLatch (java.util.concurrent.CountDownLatch)757 IOException (java.io.IOException)734 Callable (java.util.concurrent.Callable)634 ExecutionException (java.util.concurrent.ExecutionException)564 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)366 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)322 HashMap (java.util.HashMap)275 List (java.util.List)270 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)249 Test (org.testng.annotations.Test)244 TimeoutException (java.util.concurrent.TimeoutException)223 Map (java.util.Map)217 File (java.io.File)213 HashSet (java.util.HashSet)190 Test (org.junit.jupiter.api.Test)174 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)170