Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

Introduction

In this page you can find the example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor.

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters, the default thread factory and the default rejected execution handler.

Usage

From source file:org.mzd.shap.spring.task.PooledTaskExecutor.java

/**
 * Initialization method. This must be called before using the instance.
 * /*  w ww.j  a  va2 s  .  co  m*/
 * @throws ApplicationException
 */
public void init() throws ApplicationException {
    LOGGER.info("Initializing instance");
    if (getThreadPool() != null) {
        throw new ApplicationException("This instance has already been initialized");
    }
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(getQueueCapacity());
    setThreadPool(new ThreadPoolExecutor(getCorePoolSize(), getMaximumPoolSize(), getKeepAliveTime(),
            TimeUnit.SECONDS, queue));
    getThreadPool().allowCoreThreadTimeOut(true);
}

From source file:org.apache.jmeter.report.processor.ExternalSampleSorter.java

public ExternalSampleSorter() {
    chunkSize = DEFAULT_CHUNK_SIZE;/*from   w  w  w .  j  a  v  a 2s .  c  om*/
    this.nbProcessors = Runtime.getRuntime().availableProcessors();
    this.parallelize = nbProcessors > 1;
    this.pool = new ThreadPoolExecutor(nbProcessors, nbProcessors + 5, 10, TimeUnit.SECONDS, workQueue);
    setRevertedSort(false);
}

From source file:org.aksw.xoperator.Agent.java

private void registerComponents(MutablePicoContainer container) {
    container.addComponent(Controller.class, Controller.class,
            new Parameter[] { new ComponentParameter(Command.class, false), new BasicComponentParameter(),
                    new BasicComponentParameter(), new BasicComponentParameter(),
                    new BasicComponentParameter() });
    container.addComponent(loadConfigurations());
    container.addComponent(JabberClientManager.class);
    container.addComponent(SparqlEndpointFacade.class);
    container.addComponent(SimpleXslTransformer.class);
    container.addComponent(AimlFacade.class);
    container.addComponent(ExternalListenerGroovy.class);
    container.addComponent(SimpleSecurityProvider.class);
    container.addComponent(AliceBotFactory.class, AliceBotFactory.class, new Parameter[] {
            new BasicComponentParameter(), new ComponentParameter(ExternalListener.class, false) });
    container.addComponent(LocalSparqlEndpoint.class);
    container.addComponent(RemoteSparqlEndpoint.class);
    container.addComponent(RosterManager.class);
    container.addComponent(P2PFacade.class);
    container.addComponent(/*from ww  w  .ja v  a 2  s .  c om*/
            new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100)));
}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testAssignMultiTask() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });/*from   w  w w. jav  a  2 s  .  co  m*/
    workerPool.assignTask(getSleepIncrementTask());
    workerPool.assignTask(getSleepIncrementTask());
    workerPool.assignTask(getSleepIncrementTask());
    Thread.sleep(1500);
    // Only expect 2 to finish because the pool size is 2
    Assert.assertEquals(2, SleepIncrementTask.executionCounter.intValue());
    Assert.assertEquals(2, idleWorkersSemaphore.availablePermits());
    Thread.sleep(1000);
}

From source file:com.laurencedawson.image_management.ImageManager.java

/**
 * Initialize a newly created ImageManager
 * @param context The application ofinal r activity context
 * @param cacheSize The size of the LRU cache
 * @param threads The number of threads for the pools to use
 *///from  ww w . ja v  a  2 s .c o  m
public ImageManager(final Context context, final int cacheSize, final int threads) {

    // Instantiate the three queues. The task queue uses a custom comparator to 
    // change the ordering from FIFO (using the internal comparator) to respect
    // request priorities. If two requests have equal priorities, they are 
    // sorted according to creation date
    mTaskQueue = new PriorityBlockingQueue<Runnable>(QUEUE_SIZE, new ImageThreadComparator());
    mActiveTasks = new ConcurrentLinkedQueue<Runnable>();
    mBlockedTasks = new ConcurrentLinkedQueue<Runnable>();

    // The application context
    mContext = context;

    // Create a new threadpool using the taskQueue
    mThreadPool = new ThreadPoolExecutor(threads, threads, Long.MAX_VALUE, TimeUnit.SECONDS, mTaskQueue) {

        @Override
        protected void beforeExecute(final Thread thread, final Runnable run) {
            // Before executing a request, place the request on the active queue
            // This prevents new duplicate requests being placed in the active queue
            mActiveTasks.add(run);
            super.beforeExecute(thread, run);
        }

        @Override
        protected void afterExecute(final Runnable r, final Throwable t) {
            // After a request has finished executing, remove the request from
            // the active queue, this allows new duplicate requests to be submitted
            mActiveTasks.remove(r);

            // Perform a quick check to see if there are any remaining requests in
            // the blocked queue. Peek the head and check for duplicates in the 
            // active and task queues. If no duplicates exist, add the request to
            // the task queue. Repeat this until a duplicate is found
            synchronized (mBlockedTasks) {
                while (mBlockedTasks.peek() != null && !mTaskQueue.contains(mBlockedTasks.peek())
                        && !mActiveTasks.contains(mBlockedTasks.peek())) {
                    Runnable runnable = mBlockedTasks.poll();
                    if (runnable != null) {
                        mThreadPool.execute(runnable);
                    }
                }
            }
            super.afterExecute(r, t);
        }
    };

    // Calculate the cache size
    final int actualCacheSize = ((int) (Runtime.getRuntime().maxMemory() / 1024)) / cacheSize;

    // Create the LRU cache
    // http://developer.android.com/reference/android/util/LruCache.html

    // The items are no longer recycled as they leave the cache, turns out this wasn't the right
    // way to go about this and often resulted in recycled bitmaps being drawn
    // http://stackoverflow.com/questions/10743381/when-should-i-recycle-a-bitmap-using-lrucache
    mBitmapCache = new LruCache<String, Bitmap>(actualCacheSize) {
        protected int sizeOf(final String key, final Bitmap value) {
            return value.getByteCount() / 1024;
        }
    };
}

From source file:com.pinterest.rocksplicator.controller.DispatcherTest.java

@Test
public void testingMultiTasks() throws Exception {
    sleepTimeMillis = 3000;/*from  www  .  j av  a 2  s. c o m*/
    PowerMockito.when(taskQueue.dequeueTask(anyString())).thenReturn(getSleepIncrementTaskFromQueue())
            .thenReturn(getSleepIncrementTaskFromQueue()).thenReturn(getSleepIncrementTaskFromQueue())
            .thenReturn(null);
    sleepTimeMillis = 3000;
    Semaphore idleWorkersSemaphore = new Semaphore(2);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(2));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, taskQueue);
    TaskDispatcher dispatcher = new TaskDispatcher(2, idleWorkersSemaphore, workerPool, taskQueue);
    dispatcher.start();
    synchronized (SleepIncrementTask.notifyObject) {
        SleepIncrementTask.notifyObject.wait();
        SleepIncrementTask.notifyObject.wait();
    }
    Assert.assertTrue(SleepIncrementTask.executionCounter.intValue() <= 3);
    Assert.assertTrue(SleepIncrementTask.executionCounter.intValue() >= 2);
    dispatcher.stop();
}

From source file:org.sourceopen.hadoop.hbase.replication.consumer.FileChannelManager.java

public void start() throws Exception {
    init();// ww  w .  j a  v  a  2 s.  c o m
    scanProducerFilesAndAddToZK();
    FileChannelRunnable runn;
    dataLoadingManager.setConf(conf);
    dataLoadingManager.init();
    for (int i = 1; i < conf.getInt(ConsumerConstants.CONFKEY_REP_FILE_CHANNEL_POOL_SIZE, 10); i++) {
        runn = new FileChannelRunnable(conf, dataLoadingManager, adapter, stopflag);
        runn.setZoo(zoo);
        fileChannelPool.execute(runn);
    }

    crushPool = new ThreadPoolExecutor(
            conf.getInt(ProducerConstants.CONFKEY_REP_REJECT_POOL_SIZE, ProducerConstants.REP_REJECT_POOL_SIZE),
            conf.getInt(ProducerConstants.CONFKEY_REP_REJECT_POOL_SIZE, ProducerConstants.REP_REJECT_POOL_SIZE),
            conf.getInt(ProducerConstants.CONFKEY_THREADPOOL_KEEPALIVE_TIME, 100), TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(conf.getInt(ProducerConstants.CONFKEY_THREADPOOL_SIZE, 100)));
    CrushScanner crush;
    for (int i = 0; i < conf.getInt(ProducerConstants.CONFKEY_REP_REJECT_POOL_SIZE,
            ProducerConstants.REP_REJECT_POOL_SIZE); i++) {
        crush = CrushScanner.newInstance(conf, zoo);
        crushPool.execute(crush);
    }

    while (true) {
        try {
            Thread.sleep(5000);
            scanProducerFilesAndAddToZK();
        } catch (IOException e) {
            if (LOG.isErrorEnabled()) {
                LOG.error("scanProducerFilesAndAddToZK error while looping.", e);
            }
        } catch (InterruptedException e) {
            LOG.warn("FileChannelManager sleep interrupted.Break the loop!");
            break;
        }
    }
}

From source file:org.apache.drill.optiq.EnumerableDrill.java

@Override
public Enumerator<E> enumerator() {
    // TODO: use a completion service from the container
    final ExecutorCompletionService<Collection<RunOutcome>> service = new ExecutorCompletionService<Collection<RunOutcome>>(
            new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(10)));

    // Run the plan using an executor. It runs in a different thread, writing
    // results to our queue.
    ///*from   w ww. j  a v a 2 s .  c  o m*/
    // TODO: use the result of task, and check for exceptions
    final Future<Collection<RunOutcome>> task = runPlan(service);

    return new Enumerator<E>() {
        private E current;

        @Override
        public E current() {
            return current;
        }

        @Override
        public boolean moveNext() {
            try {
                Object o = queue.take();
                if (o instanceof RunOutcome.OutcomeType) {
                    switch ((RunOutcome.OutcomeType) o) {
                    case SUCCESS:
                        return false; // end of data
                    case CANCELED:
                        throw new RuntimeException("canceled");
                    case FAILED:
                    default:
                        throw new RuntimeException("failed");
                    }
                } else {
                    current = (E) parseJson((byte[]) o);
                    return true;
                }
            } catch (InterruptedException e) {
                Thread.interrupted();
                throw new RuntimeException(e);
            }
        }

        @Override
        public void reset() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.apache.hive.hcatalog.templeton.JobRequestExecutor.java

public JobRequestExecutor(JobRequestType requestType, String concurrentRequestsConfigName,
        String jobTimeoutConfigName, boolean enableCancelTask) {

    this.concurrentRequestsConfigName = concurrentRequestsConfigName;
    this.jobTimeoutConfigName = jobTimeoutConfigName;
    this.requestType = requestType;
    this.enableCancelTask = enableCancelTask;

    /*/*from ww  w.j a v a 2s .c o m*/
     * The default number of threads will be 0. That means thread pool is not used and
     * operation is executed with the current thread.
     */
    int threads = !StringUtils.isEmpty(concurrentRequestsConfigName)
            ? appConf.getInt(concurrentRequestsConfigName, 0)
            : 0;

    if (threads > 0) {
        /*
         * Create a thread pool with no queue wait time to execute the operation. This will ensure
         * that job requests are rejected if there are already maximum number of threads busy.
         */
        this.jobExecutePool = new ThreadPoolExecutor(threads, threads, threadKeepAliveTimeInHours,
                TimeUnit.HOURS, new SynchronousQueue<Runnable>());
        this.jobExecutePool.allowCoreThreadTimeOut(true);

        /*
         * Get the job request time out value. If this configuration value is set to 0
         * then job request will wait until it finishes.
         */
        if (!StringUtils.isEmpty(jobTimeoutConfigName)) {
            this.requestExecutionTimeoutInSec = appConf.getInt(jobTimeoutConfigName, 0);
        }

        LOG.info("Configured " + threads + " threads for job request type " + this.requestType
                + " with time out " + this.requestExecutionTimeoutInSec + " s.");
    } else {
        /*
         * If threads are not configured then they will be executed in current thread itself.
         */
        LOG.info("No thread pool configured for job request type " + this.requestType);
    }
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

private ExecutorService createThreadPool() {
    assert getGlobalConfiguration() != null;

    ExecutorService result = new ThreadPoolExecutor(
            getGlobalConfiguration().getBatchRequestsMinThreadsPoolSize(),
            getGlobalConfiguration().getBatchRequestsMaxThreadsPoolSize(),
            getGlobalConfiguration().getBatchRequestsThreadKeepAliveSeconds(), TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    return result;
}