Here you can find the source of createBoundedCachedThreadPool( final int corePoolSize, final int maximumPoolSize, final long keepAliveTime, final TimeUnit timeUnit)
Parameter | Description |
---|---|
corePoolSize | The core number of threads. |
maximumPoolSize | The maximum allowed number of threads. |
keepAliveTime | The time limit for which threads may remain idle before they being terminated. |
timeUnit | The time unit of the keepAliveTime argument. |
private static ThreadPoolExecutor createBoundedCachedThreadPool( final int corePoolSize, final int maximumPoolSize, final long keepAliveTime, final TimeUnit timeUnit)
//package com.java2s; /*/*from w w w .java2 s . c o 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.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class Main { /** * 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); } }