import java.util.Vector;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.nufront.euht.web.charts.data.cpu.business.EbuPerformanceThread;
import com.nufront.euht.web.common.util.MyTimeUtil;
public class SnmpThreadPool {
private int threadNumber;
private Vector<Thread> runningVector = new Vector<Thread>();
private LinkedBlockingQueue<Thread> waitingQueue = new LinkedBlockingQueue<Thread>();
private final int SLEEP = 100;
private int number = 0;
private Log log = LogFactory.getLog(SnmpThreadPool.class);
public SnmpThreadPool(int threadNumber){
log.debug("运行EbuSnmpThreadPool构造方法");
this.threadNumber = threadNumber;
Thread thread = new Thread(new ThreadCheck());
thread.setName("ThreadCheck Thread");
thread.start();
}
public synchronized void execute(Thread thread){
waitingQueue.add(thread);
}
class ThreadCheck implements Runnable{
private Log threadCheckLog = LogFactory.getLog(ThreadCheck.class);
@Override
public void run() {
threadCheckLog.debug("运行ThreadCheck run方法");
while(true){
try {
Thread.sleep(SLEEP);
if(runningVector.size() > 0){
for(int i = 0 ; i < runningVector.size() ; i++){
Thread current = runningVector.get(i);
if(!current.isAlive()){
runningVector.remove(i);
}
}
}
number = threadNumber - runningVector.size();
if(number>0){
for(int i = 0 ; i < number ;i++){
if(waitingQueue.size()>0){
Thread newThread = waitingQueue.poll();
runningVector.add(newThread);
newThread.start();
// Thread.sleep(500);
}
}
}
//测试代码
if(waitingQueue.size()==0){
log.debug("结束时间为 : "+ MyTimeUtil.getNowTime());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}