001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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     */
016    package org.kuali.common.threads;
017    
018    import java.text.NumberFormat;
019    import java.util.List;
020    
021    import org.slf4j.Logger;
022    import org.slf4j.LoggerFactory;
023    
024    public class ThreadInvoker {
025        private final Logger logger = LoggerFactory.getLogger(ThreadHandlerFactory.class);
026        ThreadHandlerFactory factory = new ThreadHandlerFactory();
027        NumberFormat nf = getNumberFormat();
028    
029        protected NumberFormat getNumberFormat() {
030            NumberFormat nf = NumberFormat.getInstance();
031            nf.setMaximumFractionDigits(2);
032            nf.setMinimumFractionDigits(2);
033            nf.setGroupingUsed(false);
034            return nf;
035        }
036    
037        public <T> ExecutionStatistics invokeThreads(ThreadHandlerContext<T> context) {
038            return invokeThreads(context, "Executing ");
039        }
040    
041        public <T> ExecutionStatistics invokeThreads(ThreadHandlerContext<T> context, String msg) {
042            if (context.getHandler() == null || context.getList() == null) {
043                throw new IllegalArgumentException("Neither elementHandler nor list can be null");
044            }
045            ThreadHandler<T> handler = factory.getThreadHandler(context);
046            int size = context.getList().size();
047            int threads = handler.getThreadCount();
048            double elementsPerThread = (size * 1D) / threads;
049            logger.info(msg + "[t:" + threads + " e:" + nf.format(elementsPerThread) + " s:" + size + "]");
050            handler.executeThreads();
051            return handler.getExecutionStatistics();
052        }
053    
054        public <T> ExecutionStatistics invokeThreads(int max, int min, int divisor, ThreadHandlerContext<T> context) {
055            context.setMax(max);
056            context.setMin(min);
057            context.setDivisor(divisor);
058            return invokeThreads(context);
059        }
060    
061        public <T> ExecutionStatistics invokeThreads(int max, ElementHandler<T> elementHandler, List<T> list) {
062            ThreadHandlerContext<T> thc = new ThreadHandlerContext<T>();
063            thc.setMax(max);
064            thc.setHandler(elementHandler);
065            thc.setList(list);
066            return invokeThreads(max, 0, 0, thc);
067        }
068    
069    }