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:com.ibuildapp.romanblack.CataloguePlugin.imageloader.Plugin.java

private Plugin() {
    final int CORES = Runtime.getRuntime().availableProcessors();
    final int THREAD_POOL_SIZE_NETWORK = CORES + 1;
    final int THREAD_POOL_SIZE_NETWORK_MAX = CORES * 2 + 1;
    final long KEEP_ALIVE_VALUE = 1;
    final TimeUnit KEEP_ALIVE_VALUE_TIME_UNIT = TimeUnit.SECONDS;
    final int CACHE_BITMAP_SIZE = (int) (Runtime.getRuntime().maxMemory() / 8192f);

    cacheBitmap = new LruCache<String, Bitmap>(CACHE_BITMAP_SIZE) {
        @Override/*from w w  w. j a  v  a  2 s  .  c o  m*/
        protected int sizeOf(String key, Bitmap bitmap) {
            return (int) ((bitmap.getRowBytes() * bitmap.getHeight()) / 1024f);
        }
    };

    queueNetwork = new LinkedBlockingQueue<>();
    queueLocal = new LinkedBlockingQueue<>();

    threadPoolNetwork = new ThreadPoolExecutor(THREAD_POOL_SIZE_NETWORK, THREAD_POOL_SIZE_NETWORK_MAX,
            KEEP_ALIVE_VALUE, KEEP_ALIVE_VALUE_TIME_UNIT, queueNetwork);
    threadPoolLocal = new ThreadPoolExecutor(CORES, CORES, KEEP_ALIVE_VALUE, KEEP_ALIVE_VALUE_TIME_UNIT,
            queueLocal);
}

From source file:org.jmangos.commons.threadpool.CommonThreadPoolManager.java

/**
 * @see org.jmangos.commons.service.Service#start()
 *///from w  w  w.  j  ava2s . c o  m
@PostConstruct
@Override
public void start() {

    final int scheduledPoolSize = ThreadPoolConfig.GENERAL_POOL;
    this.scheduledPool = new ScheduledThreadPoolExecutor(scheduledPoolSize);
    this.scheduledPool.prestartAllCoreThreads();

    final int instantPoolSize = ThreadPoolConfig.GENERAL_POOL;
    this.instantPool = new ThreadPoolExecutor(instantPoolSize, instantPoolSize, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(100000));
    this.instantPool.prestartAllCoreThreads();
}

From source file:org.nuxeo.ecm.core.event.pipe.QueueBaseEventBundlePipe.java

@Override
public void initPipe(String name, Map<String, String> params) {
    super.initPipe(name, params);
    stop = false;/* www  .ja v  a 2s . c  om*/

    if (params.containsKey("batchSize")) {
        try {
            batchSize = Integer.parseInt(params.get(batchSize));
        } catch (NumberFormatException e) {
            log.error("Unable to read batchSize parameter", e);
        }
    }
    queue = new ConcurrentLinkedQueue<>();
    consumerTPE = new ThreadPoolExecutor(1, 1, 60, TimeUnit.MINUTES, new LinkedBlockingQueue<>());
    consumerTPE.prestartCoreThread();
    consumerTPE.execute(new Runnable() {

        private boolean send(List<EventBundle> messages) {
            if (consumer.receiveMessage(messages)) {
                messages.clear();
                return true;
            }

            // keep the events that can not be processed ?
            queue.addAll(messages);
            return false;
        }

        @Override
        public void run() {

            consumer = new LocalEventBundlePipeConsumer();
            consumer.initConsumer(getName(), getParameters());
            boolean interrupted = false;
            try {
                while (!stop) {
                    List<EventBundle> messages = new ArrayList<>();
                    EventBundle message;
                    while ((message = queue.poll()) != null) {
                        messages.add(message);
                        if (messages.size() >= batchSize) {
                            send(messages);
                        }
                    }
                    if (messages.size() > 0) {
                        send(messages);
                    }

                    // XXX this is a hack ! TODO: find a better approach
                    try {
                        if (Framework.isTestModeSet()) {
                            Thread.sleep(5);
                        } else {
                            Thread.sleep(200);
                        }
                    } catch (InterruptedException e) {
                        interrupted = true;
                    }
                }
            } finally {
                if (interrupted) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    });
    consumerTPE.shutdown();
}

From source file:com.ibuildapp.ZopimChatPlugin.core.Core.java

Core() {
    final int CORES = Runtime.getRuntime().availableProcessors();
    final int THREAD_POOL_SIZE_NETWORK = CORES + 1;
    final int THREAD_POOL_SIZE_NETWORK_MAX = CORES * 2 + 1;
    final long KEEP_ALIVE_VALUE = 1;
    final TimeUnit KEEP_ALIVE_VALUE_TIME_UNIT = TimeUnit.SECONDS;
    final int CACHE_BITMAP_SIZE = (int) (Runtime.getRuntime().maxMemory() / 8192f);

    cacheBitmap = new LruCache<String, Bitmap>(CACHE_BITMAP_SIZE) {
        @Override/* w w  w  .ja v  a  2  s.com*/
        protected int sizeOf(String key, Bitmap bitmap) {
            return (int) ((bitmap.getRowBytes() * bitmap.getHeight()) / 1024f);
        }
    };

    queueNetwork = new LinkedBlockingQueue<>();
    queueLocal = new LinkedBlockingQueue<>();

    threadPoolNetwork = new ThreadPoolExecutor(THREAD_POOL_SIZE_NETWORK, THREAD_POOL_SIZE_NETWORK_MAX,
            KEEP_ALIVE_VALUE, KEEP_ALIVE_VALUE_TIME_UNIT, queueNetwork);
    threadPoolLocal = new ThreadPoolExecutor(CORES, CORES, KEEP_ALIVE_VALUE, KEEP_ALIVE_VALUE_TIME_UNIT,
            queueLocal);
}

From source file:com.cisco.oss.foundation.message.AbstractMessageDispatcher.java

public AbstractMessageDispatcher(ConcurrentMessageHandler concurrentMessageHandler) {
    this.concurrentMessageHandler = concurrentMessageHandler;

    Configuration configuration = ConfigurationFactory.getConfiguration();
    int maxThreadPoolSize = configuration.getInt(MessageConstants.QUEUE_SIZE_PROPERTY);
    int waitingQueueSize = configuration.getInt(MessageConstants.WAITING_QUEUE_SIZE_PROPERTY);

    waitingList = new CopyOnWriteArrayList<Message>();
    try {//from  www .  j  a  v a  2  s .  c om
        if (waitingQueueSize > 0) {
            blockingWaitingQueue = new CapacityEnsurableLinkedBlockingQueue<Runnable>(waitingQueueSize);
        } else {
            blockingWaitingQueue = new CapacityEnsurableLinkedBlockingQueue<Runnable>();
        }
    } catch (Exception ex) {
        LOGGER.error("Failed to create message dispatcher", ex);
    }
    executorService = new ThreadPoolExecutor(maxThreadPoolSize, maxThreadPoolSize, 0L, TimeUnit.MILLISECONDS,
            blockingWaitingQueue); //Executors.newFixedThreadPool(maxThreadPoolSize);
}

From source file:co.mafiagame.engine.executor.CommandExecutor.java

@PostConstruct
public void init() {
    commandsMap = new HashMap<>();
    commands.forEach(c -> commandsMap.put(c.commandName(), c));
    singleThread = new ThreadPoolExecutor(10, 10, keepAlive, TimeUnit.MINUTES, new LinkedBlockingQueue<>());
}

From source file:com.evandroid.musica.services.BatchDownloaderService.java

public BatchDownloaderService() {
    super("Batch Downloader Service");
    mDownloadThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME,
            KEEP_ALIVE_TIME_UNIT, new LinkedBlockingQueue<Runnable>());
}

From source file:com.liferay.sync.engine.lan.session.LanSession.java

public static ExecutorService getExecutorService() {
    if (_queryExecutorService != null) {
        return _queryExecutorService;
    }/*  w  ww  .jav  a 2s .  c  o m*/

    _queryExecutorService = new ThreadPoolExecutor(PropsValues.SYNC_LAN_SESSION_QUERY_POOL_MAX_SIZE,
            PropsValues.SYNC_LAN_SESSION_QUERY_POOL_MAX_SIZE, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());

    _queryExecutorService.allowCoreThreadTimeOut(true);

    return _queryExecutorService;
}

From source file:org.apache.hadoop.io.ReadaheadPool.java

private ReadaheadPool() {
    pool = new ThreadPoolExecutor(POOL_SIZE, MAX_POOL_SIZE, 3L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(CAPACITY));
    pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
    pool.setThreadFactory(//from w  w  w.ja  v a 2  s.  co m
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Readahead Thread #%d").build());
}

From source file:com.adobe.ags.curly.controller.BatchRunner.java

@Override
public void run() {
    try {/*from  w  w w.  j  av a2  s.  c om*/
        ApplicationState.getInstance().runningProperty().set(true);
        executor = new ThreadPoolExecutor(concurrency, concurrency, 1, TimeUnit.DAYS, tasks);
        executor.allowCoreThreadTimeOut(true);
        result.start();
        buildWorkerPool.run();
        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.DAYS);
        result.stop();
    } catch (InterruptedException ex) {
        Logger.getLogger(BatchRunner.class.getName()).log(Level.SEVERE, null, ex);
        if (!executor.isShutdown()) {
            executor.getQueue().clear();
        }
    }
    result.stop();
}