线程池的问题
我实现了一个线程池,可总有一些莫名其妙的错误,尤其是在linux下运行时出错可能性更大,请各位帮我看看。思路是这样的:建一个线程池,一个任务队列,每个线程无限循环地从任务队列中获取任务(一个字符串)并完成(打印)任务。如果任务队列为空,则线程被阻塞。当往任务队列中添加了一个任务时再激活在任务队列上等待的某个任务。最后中断线程池中的线程。
/**********************线程池管理器类***********************************/
import java.util.ArrayList;
public class ThreadPoolManager {
private final int MAX_THREADS = 10;
private ArrayList<SimpleThread> threadPool;
private JobQueue jobQueue;
/**
* constructor,to create a pool of threads and a joblist,in the pool of threads,there
* are MAX_THREADS threads,at first,they are all waiting to get a job from the joblist
*
*/
public ThreadPoolManager(){
jobQueue = new JobQueue();
threadPool = new ArrayList<SimpleThread>();
for(int i = 0;i < MAX_THREADS;i++){
SimpleThread worker = new SimpleThread(jobQueue);
threadPool.add(worker);
worker.start();
}
}
/**
* add the task into the job queue
* @param task the job to be added
*/
public void process(Object task){
jobQueue.addJob(task);
}
/**
* close the pool of the the thread,first,the pool must wait the jobs in the job queue to be
* finished,then terminate the threads in the pool one by one
*
*/
public void close(){
int runningThreads = MAX_THREADS;
while(!jobQueue.isEmpty()){}
while(true){
if(threadPool.isEmpty()){
threadPool = null;
return;
}
for(int i = 0;i < runningThreads;i++){
if(threadPool.get(i).getState() != Thread.State.RUNNABLE){
threadPool.remove(i).interrupt();
i--;
runningThreads--;
}
}
//System.out.println(threadPool.size());
}
}
/**
* @param args
*/
public static void main(String[] args) {
ThreadPoolManager manager = new ThreadPoolManager();
for(int i = 0;i < 1000;i++){
manager.process("task" + i);
}
manager.close();
}
}
/**********************线程类***********************************/
public class SimpleThread extends Thread {
private JobQueue tasksBuffer;
public SimpleThread(JobQueue tasksBuffer) {
this.tasksBuffer = tasksBuffer;
}
public void run() {
while (true) {
Object task;
try{
task = tasksBuffer.getJob();
}catch(InterruptedException e){
return;
}
System.out.println(this.getName() + " is processing " + task);
for(int i = 0;i < 900000;i++){}
}
}
}
/**********************任务队列类***********************************/
import java.util.LinkedList;
public class JobQueue {
private LinkedList<Object> jobList;
/**
* constructor,to create a job queue
*
*/
public JobQueue() {
jobList = new LinkedList<Object>();
}
/**
* to add a job into the job queue,after this,awake a waiting thread
*
* @param task
* a job to be added
*/
public void addJob(Object task) {
synchronized (jobList) {
jobList.add(task);
jobList.notify();
}
}
/**
* to get a job from the job queue,if the job queue is empty,the let the
* thread trying to get a job wait
*
* @return a job
*/
public Object getJob() throws InterruptedException{
synchronized (jobList) {
if (jobList.isEmpty()) {
jobList.wait();//有时候这个阻塞语句为什么不起作用?????????
}
return jobList.remove(0);
}
}
/*
* judge whether the jobs hava been finished,if finished,return true,else return false
*/
public boolean isEmpty(){
return jobList.isEmpty();
}
}