Java tutorial
/* * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group. * * 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. */ package com.roncoo.pay.app.settlement.utils; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * www.roncoo.com * @author zenghao */ public class SettThreadPoolExecutor { private static final Log LOG = LogFactory.getLog(SettThreadPoolExecutor.class); /** * ThreadMax 1080.8 */ private int notifyRadio = 4; /** * .<br/> * ???corePoolSize?. */ private int corePoolSize; /** * ?.<br/> * ??corePoolSizeworkQueue?workQueue??. */ private int workQueueSize; /** * .<br/> * workQueue??<br/> * ??maximumPoolSizeRejectedExecutionHandler????. */ private int maxPoolSize; /** * ?,??.<br/> * ?corePoolSizekeepAliveTime???. */ private long keepAliveTime; private ThreadPoolExecutor executor = null; public void init() { if (workQueueSize < 1) { workQueueSize = 1000; } if (this.keepAliveTime < 1) { this.keepAliveTime = 1000; } int coreSize = 0; if (this.corePoolSize < 1) { coreSize = Runtime.getRuntime().availableProcessors(); maxPoolSize = Math.round(((float) (coreSize * notifyRadio)) / 10); corePoolSize = coreSize / 4; if (corePoolSize < 1) { corePoolSize = 1; } } // NOTICE: corePoolSize?maxPoolSize? if (maxPoolSize < corePoolSize) { maxPoolSize = corePoolSize; } /** * ThreadPoolExecutor??BlockingQueue????workQueue.take()? */ BlockingQueue<Runnable> notifyWorkQueue = new ArrayBlockingQueue<Runnable>(workQueueSize); executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, notifyWorkQueue, new ThreadPoolExecutor.CallerRunsPolicy()); LOG.info("NotifyExecutor Info : CPU = " + coreSize + " | corePoolSize = " + corePoolSize + " | maxPoolSize = " + maxPoolSize + " | workQueueSize = " + workQueueSize); } public void destroy() { executor.shutdownNow(); } public void execute(Runnable command) { executor.execute(command); } public void showExecutorInfo() { LOG.info("NotifyExecutor Info : corePoolSize = " + corePoolSize + " | maxPoolSize = " + maxPoolSize + " | workQueueSize = " + workQueueSize + " | taskCount = " + executor.getTaskCount() + " | activeCount = " + executor.getActiveCount() + " | completedTaskCount = " + executor.getCompletedTaskCount()); } public void setNotifyRadio(int notifyRadio) { this.notifyRadio = notifyRadio; } public void setWorkQueueSize(int workQueueSize) { this.workQueueSize = workQueueSize; } public void setKeepAliveTime(long keepAliveTime) { this.keepAliveTime = keepAliveTime; } public void setCorePoolSize(int corePoolSize) { this.corePoolSize = corePoolSize; } public void setMaxPoolSize(int maxPoolSize) { this.maxPoolSize = maxPoolSize; } }