Java 8 ThreadPoolExecutor - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 ThreadPoolExecutor class.

This code example shows how to use the following methods:execute, getActiveCount, getQueue, getTaskCount
  synchronized void execute(Runnable task) {
    if (executor == null) {
      throw new RuntimeException("AsyncDataService is already shutdown");
    } 
    if (LOG.isDebugEnabled()) {
      LOG.debug("Current active thread number: " + executor.getActiveCount()
          + " queue size:" + executor.getQueue().size()
          + " scheduled task number:" + executor.getTaskCount());
    } 
    executor.execute(task);
  } 
 
  /** 
   * Gracefully shut down the ThreadPool. Will wait for all data tasks to 
   * finish. 
   */ 
  synchronized void shutdown() { 
    if (executor == null) {
      LOG.warn("AsyncDataService has already shut down.");
    } else { 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getQueue, isShutdown
 
    @Override 
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if (r instanceof AbstractRunnable) {
            if (((AbstractRunnable) r).isForceExecution()) {
                BlockingQueue<Runnable> queue = executor.getQueue();
                if (!(queue instanceof SizeBlockingQueue)) {
                    throw new ElasticSearchIllegalStateException("forced execution, but expected a size queue"); 
                } 
                try { 
                    ((SizeBlockingQueue) queue).forcePut(r);
                } catch (InterruptedException e) {
                    throw new ElasticSearchInterruptedException(e.getMessage(), e);
                } 
                return; 
            } 
        } 
        rejected.inc();
        StringBuilder sb = new StringBuilder("rejected execution ");
        if (executor.isShutdown()) {

8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:allowCoreThreadTimeOut
        SERVER_MANAGEMENT_LOGGER.debugf("Starting operation handler service %s", context.getController().getName());
        if(executor.getOptionalValue() == null) {
            // Create the default executor 
            final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(WORK_QUEUE_SIZE);
            final ThreadFactory threadFactory = new JBossThreadFactory(new ThreadGroup(getThreadGroupName()), Boolean.FALSE, null, "%G - %t", null, null, doPrivileged(GetAccessControlContextAction.getInstance()));
            final ThreadPoolExecutor executorService = new ThreadPoolExecutor(POOL_CORE_SIZE, POOL_MAX_SIZE,
                                                            60L, TimeUnit.SECONDS, workQueue,
                                                            threadFactory);
            // Allow the core threads to time out as well 
            executorService.allowCoreThreadTimeOut(true);
            getExecutorInjector().inject(executorService);
        } 
    } 
 
    /** {@inheritDoc} */ 
    @Override 
    public synchronized void stop(StopContext context) {
        // 
    } 
 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:awaitTermination, shutdown, shutdownNow
        } 
    } 
 
    @Override 
    public void stop() throws Exception { 
        executor.shutdown();
        if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
            executor.shutdownNow();
        } 
    } 
 
    @Override 
    public void addLifeCycleListener(Listener listener) {
        System.err.println("we should implement this!");
    } 
 
    @Override 
    public void removeLifeCycleListener(Listener listener) {
        System.err.println("we should implement this!");
    } 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getActiveCount, getPoolSize
        } 
    } 
 
    @Override 
    public int getIdleThreads() { 
        return executor.getPoolSize() - executor.getActiveCount();
    } 
 
    @Override 
    public int getThreads() { 
        return executor.getPoolSize();
    } 
 
    @Override 
    public boolean isLowOnThreads() { 
        return executor.getActiveCount() >= executor.getMaximumPoolSize();
    } 
 
    @Override 
    public void join() throws InterruptedException {
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setCorePoolSize
        this.keepAlive = keepAlive;
    } 
 
    public synchronized void start(final StartContext context) throws StartException {
        ScheduledThreadPoolExecutor scheduledExecutor = new ExecutorImpl(0, threadFactoryValue.getValue());
        scheduledExecutor.setCorePoolSize(maxThreads);
        if(keepAlive != null)
            scheduledExecutor.setKeepAliveTime(keepAlive.getDuration(), keepAlive.getUnit());
        executor = new ManagedScheduledExecutorService(scheduledExecutor);
    } 
 
    public synchronized void stop(final StopContext context) {
        final ManagedScheduledExecutorService executor = getValue();
        this.context = context;
        context.asynchronous();
        executor.internalShutdown();
        this.executor = null;
    } 
 
    public synchronized ManagedScheduledExecutorService getValue() throws IllegalStateException { 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:awaitTermination, shutdown
 
 
    @Override 
    public void dispose() { 
        if (this.getExecutor != null) {
            this.getExecutor.shutdown();
        } 
 
        if (this.unOrderedPutExecutor != null) {
            this.unOrderedPutExecutor.shutdown();
        } 
        try { 
            this.getExecutor.awaitTermination(5000, TimeUnit.MILLISECONDS);
            this.unOrderedPutExecutor.awaitTermination(5000, TimeUnit.MILLISECONDS);
        } 
        catch (InterruptedException e) {
            // ignore 
        } 
    } 
 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getActiveCount, getCompletedTaskCount, getCorePoolSize, getLargestPoolSize, getMaximumPoolSize
    @Override 
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        String msg = String.format("Thread pool is EXHAUSTED!" +
                " Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d)," + 
                " Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!" , 
                threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(),
                e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
                url.getProtocol(), url.getIp(), url.getPort());
        logger.warn(msg);
        throw new RejectedExecutionException(msg);
    } 
 
} 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getQueue, isShutdown
 
    @Override 
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if (r instanceof AbstractRunnable) {
            if (((AbstractRunnable) r).isForceExecution()) {
                BlockingQueue<Runnable> queue = executor.getQueue();
                if (!(queue instanceof SizeBlockingQueue)) {
                    throw new ElasticsearchIllegalStateException("forced execution, but expected a size queue"); 
                } 
                try { 
                    ((SizeBlockingQueue) queue).forcePut(r);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new ElasticsearchIllegalStateException("forced execution, but got interrupted", e);
                } 
                return; 
            } 
        } 
        rejected.inc();
        StringBuilder sb = new StringBuilder("rejected execution ");

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getActiveCount, getCompletedTaskCount, getCorePoolSize, getLargestPoolSize, getMaximumPoolSize
        } 
    } 
 
    @Override 
    public synchronized void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        long completed = executor.getCompletedTaskCount();
        if (completed < last_completed) {
            throw new IllegalStateException("Number of completed tasks shouldn't decrease");
        } else if (completed == last_completed) {
            long now = System.currentTimeMillis();
            if (now - last_change > period) {
                String message = String.format(
                        "No progress for %d ms, possible distributed deadlock. Try raising threadpool size\n" + 
                        "\tMin size: %d\n\tMax size: %d\n\tCurrent size: %d\n\tActive: %d\n\tLargest size: %d\n" + 
                        "\tCompleted tasks: %d\n\tTotal scheduled: %d", 
                        now - last_change, executor.getCorePoolSize(), executor.getMaximumPoolSize(),
                        executor.getPoolSize(), executor.getActiveCount(), executor.getLargestPoolSize(),
                        executor.getCompletedTaskCount(), executor.getTaskCount());
                throw new NoProgressException(message);
            } 

7
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Java 8 ThreadPoolExecutor Questions & Answers:

See Code Examples for Java 8 ThreadPoolExecutor Methods: