Search in sources :

Example 46 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 47 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)

Example 48 with ThreadPoolExecutor

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

the class FsDatasetAsyncDiskService method addExecutorForVolume.

private void addExecutorForVolume(final FsVolumeImpl volume) {
    ThreadFactory threadFactory = new ThreadFactory() {

        int counter = 0;

        @Override
        public Thread newThread(Runnable r) {
            int thisIndex;
            synchronized (this) {
                thisIndex = counter++;
            }
            Thread t = new Thread(threadGroup, r);
            t.setName("Async disk worker #" + thisIndex + " for volume " + volume);
            return t;
        }
    };
    ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_THREADS_PER_VOLUME, MAXIMUM_THREADS_PER_VOLUME, THREADS_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
    // This can reduce the number of running threads
    executor.allowCoreThreadTimeOut(true);
    executors.put(volume.getStorageID(), executor);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 49 with ThreadPoolExecutor

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

the class DisruptorReference method resizeThreadPoolExecutor.

private void resizeThreadPoolExecutor(final int newSize) {
    if (executor == null && newSize > 0) {
        LOGGER.debug("Creating new executor with {} threads", newSize);
        //no thread pool executor yet, create a new one
        executor = component.getCamelContext().getExecutorServiceManager().newFixedThreadPool(this, uri, newSize);
    } else if (executor != null && newSize <= 0) {
        LOGGER.debug("Shutting down executor");
        //we need to shut down our executor
        component.getCamelContext().getExecutorServiceManager().shutdown(executor);
        executor = null;
    } else if (executor instanceof ThreadPoolExecutor) {
        LOGGER.debug("Resizing existing executor to {} threads", newSize);
        //our thread pool executor is of type ThreadPoolExecutor, we know how to resize it
        final ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
        threadPoolExecutor.setCorePoolSize(newSize);
        threadPoolExecutor.setMaximumPoolSize(newSize);
    } else if (newSize > 0) {
        LOGGER.debug("Shutting down old and creating new executor with {} threads", newSize);
        //hmmm...no idea what kind of executor this is...just kill it and start fresh
        component.getCamelContext().getExecutorServiceManager().shutdown(executor);
        executor = component.getCamelContext().getExecutorServiceManager().newFixedThreadPool(this, uri, newSize);
    }
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 50 with ThreadPoolExecutor

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

the class DefaultExecutorServiceManager method doShutdown.

private boolean doShutdown(ExecutorService executorService, long shutdownAwaitTermination, boolean failSafe) {
    if (executorService == null) {
        return false;
    }
    boolean warned = false;
    // we ought to shutdown much faster)
    if (!executorService.isShutdown()) {
        StopWatch watch = new StopWatch();
        LOG.trace("Shutdown of ExecutorService: {} with await termination: {} millis", executorService, shutdownAwaitTermination);
        executorService.shutdown();
        if (shutdownAwaitTermination > 0) {
            try {
                if (!awaitTermination(executorService, shutdownAwaitTermination)) {
                    warned = true;
                    LOG.warn("Forcing shutdown of ExecutorService: {} due first await termination elapsed.", executorService);
                    executorService.shutdownNow();
                    // we are now shutting down aggressively, so wait to see if we can completely shutdown or not
                    if (!awaitTermination(executorService, shutdownAwaitTermination)) {
                        LOG.warn("Cannot completely force shutdown of ExecutorService: {} due second await termination elapsed.", executorService);
                    }
                }
            } catch (InterruptedException e) {
                warned = true;
                LOG.warn("Forcing shutdown of ExecutorService: {} due interrupted.", executorService);
                // we were interrupted during shutdown, so force shutdown
                executorService.shutdownNow();
            }
        }
        // if we logged at WARN level, then report at INFO level when we are complete so the end user can see this in the log
        if (warned) {
            LOG.info("Shutdown of ExecutorService: {} is shutdown: {} and terminated: {} took: {}.", executorService, executorService.isShutdown(), executorService.isTerminated(), TimeUtils.printDuration(watch.taken()));
        } else if (LOG.isDebugEnabled()) {
            LOG.debug("Shutdown of ExecutorService: {} is shutdown: {} and terminated: {} took: {}.", executorService, executorService.isShutdown(), executorService.isTerminated(), TimeUtils.printDuration(watch.taken()));
        }
    }
    // let lifecycle strategy be notified as well which can let it be managed in JMX as well
    ThreadPoolExecutor threadPool = null;
    if (executorService instanceof ThreadPoolExecutor) {
        threadPool = (ThreadPoolExecutor) executorService;
    } else if (executorService instanceof SizedScheduledExecutorService) {
        threadPool = ((SizedScheduledExecutorService) executorService).getScheduledThreadPoolExecutor();
    }
    if (threadPool != null) {
        for (LifecycleStrategy lifecycle : camelContext.getLifecycleStrategies()) {
            lifecycle.onThreadPoolRemove(camelContext, threadPool);
        }
    }
    // remove reference as its shutdown (do not remove if fail-safe)
    if (!failSafe) {
        executorServices.remove(executorService);
    }
    return warned;
}
Also used : SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) StopWatch(org.apache.camel.util.StopWatch)

Aggregations

ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)441 Test (org.junit.Test)87 ExecutorService (java.util.concurrent.ExecutorService)79 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)66 ThreadFactory (java.util.concurrent.ThreadFactory)45 SynchronousQueue (java.util.concurrent.SynchronousQueue)38 IOException (java.io.IOException)37 ArrayList (java.util.ArrayList)36 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)27 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)26 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)25 CountDownLatch (java.util.concurrent.CountDownLatch)25 ExecutionException (java.util.concurrent.ExecutionException)25 Future (java.util.concurrent.Future)23 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)19 Test (org.testng.annotations.Test)18 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 HashMap (java.util.HashMap)16 SizedScheduledExecutorService (org.apache.camel.util.concurrent.SizedScheduledExecutorService)16