001    package com.hammurapi.config.bootstrap;
002    
003    import java.util.concurrent.ArrayBlockingQueue;
004    import java.util.concurrent.Executor;
005    import java.util.concurrent.ThreadPoolExecutor;
006    import java.util.concurrent.TimeUnit;
007    
008    /**
009     * Simple thread pool with minimal confriguration.
010     * @author Pavel Vlasov
011     *
012     */
013    public class SimpleThreadPool implements Executor, Destroyable {
014            
015            private ThreadPoolExecutor tpe;
016            
017            public SimpleThreadPool(int size) {
018                    if (size>0) {
019                            ThreadPoolExecutor.CallerRunsPolicy rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
020                            ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(size*3);
021                            tpe = new ThreadPoolExecutor(size, (int) (size * 1.5), 10, TimeUnit.MINUTES, queue, rejectedExecutionHandler);
022                    } 
023            }
024    
025            public void execute(Runnable command) {
026                    if (tpe!=null) {
027                            tpe.execute(command);
028                    } else {
029                            command.run();
030                    }
031            }
032    
033            public void destroy()  {
034                    if (tpe!=null) {
035                            tpe.shutdown();
036                    }
037            }
038    
039    }