Java Thread Executor Pool getRecheckThreadPool()

Here you can find the source of getRecheckThreadPool()

Description

Returns the MX recheck ThreadPool.

License

Apache License

Return

The MX recheck ThreadPool

Declaration

public static synchronized ExecutorService getRecheckThreadPool() 

Method Source Code

//package com.java2s;
/*//from  ww w . ja  v a2  s. co  m
 * Copyright 2013, Carsten J?ger
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Constant 10.
     */
    public static final byte CONST_10 = 10;
    /**
     * Constant 60.
     */
    public static final byte CONST_60 = 60;
    /**
     * Global small ThreadPool used for rechecks.
     */
    private static ThreadPoolExecutor recheckThreadPool;

    /**
     * Returns the MX recheck ThreadPool.
     * This ThreadPool is NOT configurable!
     *
     * @return The MX recheck ThreadPool
     */
    public static synchronized ExecutorService getRecheckThreadPool() {
        if (recheckThreadPool == null) {
            //            System.err.println("#### CREATING recheckThreadPool");
            recheckThreadPool = createBoundedCachedThreadPool(0, CONST_10,
                    CONST_60, TimeUnit.SECONDS);
        }
        return recheckThreadPool;
    }

    /**
     * Creates a ThreadPool for running requests threads.
     *
     * This procedure combines the very fast possibility of adding new threads to the ThreadPool which can be done by using
     * a fixed ThreadPool (Executors.nexFixedThreadPool()) with the advantage of a cached ThreadPool (Executors.newCachedThreadPool())
     * to remove no longer used (idle) threads from the pool which reduces the usage of system resources.
     *
     * Setting the corePoolSize to 0 prevents the ThreadPoool to use any system resources when there is nothing to do
     * and will automatically shut down the ThreadPool after the keepAliveTime. So this can make the call to shutDownThreadPool()
     * obsolete (but this is NOT recommended!).
     *
     * @param corePoolSize The core number of threads.
     * @param maximumPoolSize The maximum allowed number of threads.
     * @param keepAliveTime The time limit for which threads may remain idle before they being terminated.
     * @param timeUnit The time unit of the keepAliveTime argument.
     * @return ThreadPoolExecutor.
     */
    private static ThreadPoolExecutor createBoundedCachedThreadPool(
            final int corePoolSize, final int maximumPoolSize,
            final long keepAliveTime, final TimeUnit timeUnit) {
        @SuppressWarnings("serial")
        final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>() {
            public boolean offer(final Runnable r) {
                if (size() > 1) {
                    return false;
                }
                return super.offer(r);
            };

            public boolean add(final Runnable r) {
                if (super.offer(r)) {
                    return true;
                } else {
                    throw new IllegalStateException();
                }
            }
        };
        RejectedExecutionHandler handler = new RejectedExecutionHandler() {
            public void rejectedExecution(final Runnable r,
                    final ThreadPoolExecutor executor) {
                queue.add(r);
            }
        };
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
                keepAliveTime, timeUnit, queue, handler);
    }
}

Related

  1. createPool(int threads, int queueSize)
  2. createPooledExecutorService(int poolSize, final String namePrefix)
  3. getPool()
  4. getPool()
  5. getQueuedThreadPool(double threadToCpuRatio, int queueCapacity)
  6. getThreadPool()
  7. getThrealPool()
  8. getTightThreadPool()
  9. normalShutdown(ExecutorService pool, int timeout, TimeUnit timeUnit)