threadpool.java
来自「彩信网关程序,SP程序,包含移动彩信代码和电信彩信代码!供学习之用」· Java 代码 · 共 73 行
JAVA
73 行
package com.hxyh.sanny.mms.cmcc.base.thread;
public class ThreadPool
{
private ObjectFIFO idleWorkers;
private ThreadPoolWorker workerList[];
public ThreadPool(int numberOfThreads)
{
numberOfThreads = Math.max(1, numberOfThreads);
idleWorkers = new ObjectFIFO(numberOfThreads);
workerList = new ThreadPoolWorker[numberOfThreads];
for(int i = 0; i < workerList.length; i++)
workerList[i] = new ThreadPoolWorker(idleWorkers);
}
public ThreadPool(int numberOfThreads, int priority)
{
numberOfThreads = Math.max(1, numberOfThreads);
idleWorkers = new ObjectFIFO(numberOfThreads);
workerList = new ThreadPoolWorker[numberOfThreads];
for(int i = 0; i < workerList.length; i++)
workerList[i] = new ThreadPoolWorker(idleWorkers, priority);
}
public void setTimeout(long l)
{
for(int i = 0; i < workerList.length; i++)
workerList[i].setTimeout(l);
}
public void execute(Runnable target)
throws InterruptedException
{
// block (forever) until a worker is available
ThreadPoolWorker worker = (ThreadPoolWorker)idleWorkers.remove();
//System.out.println("Free Workers Num:"+idleWorkers.getSize()+"/"+idleWorkers.getCapacity());
worker.process(target);
}
public void stopRequestIdleWorkers()
{
try
{
Object idle[] = idleWorkers.removeAll();
for(int i = 0; i < idle.length; i++)
((ThreadPoolWorker)idle[i]).stopRequest();
}
catch(InterruptedException x)
{
Thread.currentThread().interrupt();
}
}
public void stopRequestAllWorkers()
{
stopRequestIdleWorkers();
try
{
Thread.sleep(250L);
}
catch(InterruptedException interruptedexception) { }
for(int i = 0; i < workerList.length; i++)
if(workerList[i].isAlive())
workerList[i].stopRequest();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?