001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.util; 017 018import java.util.concurrent.ExecutorService; 019import java.util.concurrent.ScheduledExecutorService; 020import java.util.concurrent.TimeUnit; 021 022/** 023 * Factory interface for creating/obtaining named thread pools. 024 */ 025public interface ThreadPoolFactory { 026 027 /** 028 * Obtain a thread pool with the supplied name, or create and return one if no thread pool exists with that name. When 029 * finished with the thread pool, it should be {@link #releaseThreadPool released}. 030 * 031 * @param name the name of the thread pool; may not be null 032 * @return the thread pool executor; never null 033 */ 034 ExecutorService getThreadPool( String name ); 035 036 /** 037 * Signal that the supplied thread pool is no longer needed. Obtain a cached thread pool with the supplied name, or create and 038 * return one if no thread pool exists with that name. When finished with the thread pool, it should be 039 * {@link #releaseThreadPool released}. 040 * 041 * @param maxPoolSize the maximum number of threads that can be spawned by this pool. 042 * @param name the name of the thread pool; may not be null 043 * @return the thread pool executor; never null 044 */ 045 ExecutorService getCachedTreadPool( String name, int maxPoolSize ); 046 047 /** 048 * Obtain a scheduled thread pool with the supplied name, or create and return one if no thread pool exists with that name. 049 * When finished with the thread pool, it should be {@link #releaseThreadPool released}. 050 * 051 * @param name the name of the thread pool; may not be null 052 * @return the thread pool executor; never null 053 */ 054 ScheduledExecutorService getScheduledThreadPool( String name ); 055 056 /** 057 * Performs a {@link java.util.concurrent.ExecutorService#shutdownNow()} on the given pool, if the pool has been created 058 * previously by this class. Clients which use this method should handle, if necessary, any potential 059 * {@link InterruptedException} 060 * 061 * @param pool the pool that is no longer needed 062 */ 063 void releaseThreadPool( ExecutorService pool ); 064 065 /** 066 * Terminates all the existing thread pool, by waiting for them maximum {@code maxWaitTimeMillis} milliseconds, after which 067 * calling {@link java.util.concurrent.ExecutorService#shutdownNow()}. 068 * 069 * @param maxWaitTime the maximum amount of time that should be given to the pools to shutdown on their own; must be 070 * non-negative 071 * @param timeUnit the unit of time for the {@code maxWaitTime} parameter 072 */ 073 void terminateAllPools( long maxWaitTime, 074 TimeUnit timeUnit ); 075}