Search in sources :

Example 11 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project camel by apache.

the class ThreadsRejectedExecutionTest method testThreadsRejectedExecution.

public void testThreadsRejectedExecution() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // use a custom pool which rejects any new tasks while currently in progress
            // this should force the ThreadsProcessor to run the tasks itself
            ExecutorService pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
            from("seda:start").to("log:before").threads().executorService(pool).delay(1000).to("log:after").to("mock:result");
        }
    });
    context.start();
    getMockEndpoint("mock:result").expectedMessageCount(3);
    template.sendBody("seda:start", "Hello World");
    template.sendBody("seda:start", "Hi World");
    template.sendBody("seda:start", "Bye World");
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) SynchronousQueue(java.util.concurrent.SynchronousQueue) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 12 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project camel by apache.

the class ThreadsRejectedExecutionTest method testThreadsRejectedExecutionCallerNotRuns.

public void testThreadsRejectedExecutionCallerNotRuns() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // use a custom pool which rejects any new tasks while currently in progress
            // this should force the ThreadsProcessor to run the tasks itself
            ExecutorService pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
            from("seda:start").to("log:before").threads().executorService(pool).callerRunsWhenRejected(false).delay(1000).to("log:after").to("mock:result");
        }
    });
    context.start();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(3);
    // wait at most 5 seconds
    mock.setResultWaitTime(5000);
    template.sendBody("seda:start", "Hello World");
    template.sendBody("seda:start", "Hi World");
    template.sendBody("seda:start", "Bye World");
    // should not be possible to route all 3
    mock.assertIsNotSatisfied();
    // only 1 should arrive
    assertEquals(1, mock.getReceivedCounter());
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SynchronousQueue(java.util.concurrent.SynchronousQueue) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 13 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project hadoop by apache.

the class AsyncDiskService method awaitTermination.

/**
   * Wait for the termination of the thread pools.
   * 
   * @param milliseconds  The number of milliseconds to wait
   * @return   true if all thread pools are terminated without time limit
   * @throws InterruptedException 
   */
public synchronized boolean awaitTermination(long milliseconds) throws InterruptedException {
    long end = Time.now() + milliseconds;
    for (Map.Entry<String, ThreadPoolExecutor> e : executors.entrySet()) {
        ThreadPoolExecutor executor = e.getValue();
        if (!executor.awaitTermination(Math.max(end - Time.now(), 0), TimeUnit.MILLISECONDS)) {
            LOG.warn("AsyncDiskService awaitTermination timeout.");
            return false;
        }
    }
    LOG.info("All AsyncDiskService threads are terminated.");
    return true;
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project hadoop by apache.

the class ErasureCodingWorker method initializeStripedReadThreadPool.

private void initializeStripedReadThreadPool(int num) {
    LOG.debug("Using striped reads; pool threads={}", num);
    stripedReadPool = new ThreadPoolExecutor(1, num, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new Daemon.DaemonFactory() {

        private final AtomicInteger threadIndex = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = super.newThread(r);
            t.setName("stripedRead-" + threadIndex.getAndIncrement());
            return t;
        }
    }, new ThreadPoolExecutor.CallerRunsPolicy() {

        @Override
        public void rejectedExecution(Runnable runnable, ThreadPoolExecutor e) {
            LOG.info("Execution for striped reading rejected, " + "Executing in current thread");
            // will run in the current thread
            super.rejectedExecution(runnable, e);
        }
    });
    stripedReadPool.allowCoreThreadTimeOut(true);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 15 with ThreadPoolExecutor

use of java.util.concurrent.ThreadPoolExecutor in project hadoop by apache.

the class FsDatasetAsyncDiskService method addVolume.

/**
   * Starts AsyncDiskService for a new volume
   * @param volume the root of the new data volume.
   */
synchronized void addVolume(FsVolumeImpl volume) {
    if (executors == null) {
        throw new RuntimeException("AsyncDiskService is already shutdown");
    }
    if (volume == null) {
        throw new RuntimeException("Attempt to add a null volume");
    }
    ThreadPoolExecutor executor = executors.get(volume.getStorageID());
    if (executor != null) {
        throw new RuntimeException("Volume " + volume + " is already existed.");
    }
    addExecutorForVolume(volume);
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)397 Test (org.junit.Test)79 ExecutorService (java.util.concurrent.ExecutorService)74 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)60 ThreadFactory (java.util.concurrent.ThreadFactory)38 ArrayList (java.util.ArrayList)34 IOException (java.io.IOException)33 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)30 SynchronousQueue (java.util.concurrent.SynchronousQueue)29 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)22 ExecutionException (java.util.concurrent.ExecutionException)21 Future (java.util.concurrent.Future)20 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 Test (org.testng.annotations.Test)18 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)16 SizedScheduledExecutorService (org.apache.camel.util.concurrent.SizedScheduledExecutorService)16 HashMap (java.util.HashMap)14