标题:线程池的问题
取消只看楼主
herendagao
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-5-3
结帖率:100%
已结贴  问题点数:20 回复次数:1 
线程池的问题
我实现了一个线程池,可总有一些莫名其妙的错误,尤其是在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();
    }
}
搜索更多相关主题的帖子: 线程池 linux下java开发 同步 
2009-08-21 15:48
herendagao
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-5-3
得分:0 
回复 楼主 herendagao
我知道那个好使,就是想自己做一个线程池。

2009-08-21 17:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-283370-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016124 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved