50,528
社区成员
发帖
与我相关
我的任务
分享
package org.bermuda.villain;
import java.util.Vector;
public class ThreadPool {
private static Integer pool_size = 5;
private WorkThread[] workThreads;
private static ThreadPool threadPool;
private Vector<VillainTask> taskQueue = new Vector<VillainTask>();
private Integer runFinishedTaskNum = 0;
private ThreadPool(Integer size){
if(size > 0){
ThreadPool.pool_size = size;
}
workThreads = new WorkThread[ThreadPool.pool_size];
for (int i = 0; i < ThreadPool.pool_size; i++) {
workThreads[i] = new WorkThread();
workThreads[i].start();
}
}
public static ThreadPool getThreadPoolInstance(Integer size){
if(threadPool == null){
threadPool = new ThreadPool(size);
}
return threadPool;
}
public static ThreadPool getThreadPoolInstance(){
return getThreadPoolInstance(ThreadPool.pool_size);
}
public void runTask(VillainTask task){
synchronized (taskQueue) {
taskQueue.add(task);
taskQueue.notify();
}
}
public void runTask(VillainTask[] tasks){
synchronized (taskQueue) {
for(VillainTask task : tasks){
taskQueue.add(task);
}
taskQueue.notify();
}
}
public void destroy(){
if(taskQueue != null && taskQueue.size() != 0){
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < ThreadPool.pool_size; i++) {
workThreads[i].isRunning = false;
workThreads[i] = null;
}
}
public Integer getWorkThreadNumber() {
return pool_size;
}
public int getFinishedTasknumber() {
return runFinishedTaskNum;
}
public int getWaitTasknumber() {
return taskQueue.size();
}
private synchronized void finishedTasknumber(){
runFinishedTaskNum++;
}
private class WorkThread extends Thread{
boolean isRunning = true;
@Override
public void run() {
VillainTask vTask = null;
while(isRunning){
synchronized (taskQueue) {
while (isRunning && taskQueue.isEmpty()) {
try {
taskQueue.wait(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!taskQueue.isEmpty()){
vTask = taskQueue.remove(0);
}
}
if (vTask != null) {
vTask.working();
finishedTasknumber();
}
vTask = null;
}
}
}
}
package org.bermuda.villain;
public interface VillainTask {
public void working();
}
package org.bermuda.test.villain;
import org.bermuda.villain.VillainTask;
public class TaskTest implements VillainTask {
@Override
public void working() {
for(int i=0;i<=1000;i++){
if(i == 1000){
try {
Thread.sleep(100L);
System.out.println("over done !");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package org.bermuda.test.villain;
import org.bermuda.villain.ThreadPool;
public class ThreadPoolTest {
public static void main(String[] args) throws Exception{
ThreadPool threadPool = ThreadPool.getThreadPoolInstance(10);
TaskTest[] tts = new TaskTest[100];
for(int i=0;i<tts.length;i++){
tts[i] = new TaskTest();
}
threadPool.runTask(tts);
Long l = 0L;
while(l<3000L){
l += 100L;
Thread.sleep(100L);
System.out.println(threadPool.getFinishedTasknumber());
System.out.println(threadPool.getWaitTasknumber());
System.out.println(threadPool.getWorkThreadNumber());
System.out.println("----------------------------");
}
threadPool.destroy();
}
}