001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 021 package org.apache.directory.server.protocol.shared; 022 023 import java.util.concurrent.Executor; 024 import java.util.concurrent.ExecutorService; 025 import java.util.concurrent.Executors; 026 import java.util.concurrent.TimeUnit; 027 028 /** 029 * @version $Rev: 604361 $ $Date: 2007-12-15 01:58:40 +0100 (Sat, 15 Dec 2007) $ 030 * @org.apache.xbean.XBean 031 */ 032 public class StandardThreadPool implements Executor 033 { 034 035 private final ExecutorService delegate; 036 037 038 public StandardThreadPool( int maxThreads ) 039 { 040 delegate = Executors.newFixedThreadPool( maxThreads ); 041 } 042 043 public void execute( Runnable command ) 044 { 045 delegate.execute( command ); 046 } 047 048 /** 049 * TODO wont this hang if some tasks are sufficiently badly behaved? 050 * @org.apache.xbean.DestroyMethod 051 */ 052 public void stop() 053 { 054 delegate.shutdown(); 055 for ( ; ; ) 056 { 057 try 058 { 059 if ( delegate.awaitTermination( Integer.MAX_VALUE, TimeUnit.SECONDS ) ) 060 { 061 break; 062 } 063 } 064 catch ( InterruptedException e ) 065 { 066 //ignore 067 } 068 } 069 070 } 071 }