线程池学习研究-(自实现)1

一直想好好的研究研java有关线程,并发包的东西,一直也没去做,最近因为工作中用到了,正好趁此机会好好的学习下。

看着并发包实现的线程池,功能也确实挺多的,总觉得理解的不够透彻,感觉总是别人的东西,所以决定自己从最简单的开始写,慢慢的优化,然后慢慢的对比他的源代码,这样应该能比较深入的理解。

今天开始写了个最简单的原始版本,姑且算作0.1版本,里面没有用到锁等机制。

一共分为三部分:

1、任务类(Task)

View Code
public class Task {
    public void doTask() {
        System.out.println("do the task");
    }
}

2、工作线程,调用Task任务的doTask方法

View Code
public class TaskThread extends Thread {
    private int index = -1;
    private boolean isRunning = true;
    private boolean isWaiting = true;
    private MyThreadPool pool;

    public TaskThread(MyThreadPool pool, int index) {
        this.index = index;
        this.pool = pool;
    }

    public void run() {
        while (isRunning) {
            Task task = pool.getTask();
            if (task != null) {
                task.doTask();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

3、线程池管理器,里面有一个队列,对任务进行管理

View Code
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyThreadPool {
    private BlockingQueue<Task> taskQueue;
    private BlockingQueue<TaskThread> taskThreads;

    private static int work_num;
    private static int work_count = 0;

    /**
     * 初始化,默认为5个工作线程
     */
    public MyThreadPool() {
        taskThreads = new LinkedBlockingQueue<TaskThread>(5);
        taskQueue = new LinkedBlockingQueue<Task>();
        for (int i = 0; i < 5; i++) {
            TaskThread taskThread = new TaskThread(this,i);
            taskThreads.add(taskThread);
        }
        System.out.println("taskThreads的长度为 :"+taskThreads.size());
    }

    /**
     * 带参数的初始化
     * @param coreSize
     */
    public MyThreadPool(int coreSize) {
        this.work_num = coreSize;
        taskThreads = new LinkedBlockingQueue<TaskThread>(this.work_num);
        taskQueue = new LinkedBlockingQueue<Task>();
        for (int i = 0; i < taskThreads.size(); i++) {
            TaskThread taskThread = new TaskThread(this,i);
            taskThreads.add(taskThread);
            taskThread.start();
        }
    }

    /**
     * 添加任务
     * @param task
     */
    public synchronized void addTask(Task task) {
        taskQueue.add(task);
    }

    public synchronized Task getTask() {
        if (taskQueue.size() <= 0) {
            throw new RuntimeException("队列为空");
        }
        return taskQueue.poll();
    }

    public boolean isEmpty() {
        return taskQueue.isEmpty();
    }
    
    /**
     * 工作线程执行任务
     */
    public void execute(){
        TaskThread taskThread = taskThreads.poll();
        taskThread.start();
    }

}

然后就是测试类:

View Code
public class Test {
    public static void main(String[] args) {
        MyThreadPool pool = new MyThreadPool();
        for (int i=0;i<10;i++) {
            pool.addTask(new Task());
        } 
        pool.execute();
    }
    
}

输出结果:(异常处理暂时只打印堆栈信息)

View Code
taskThreads的长度为 :5
do the task
do the task
do the task
do the task
do the task
do the task
do the task
do the task
do the task
do the task
Exception in thread "Thread-0" java.lang.RuntimeException: 队列为空
    at com.libing.threadpool.MyThreadPool.getTask(MyThreadPool.java:51)
    at com.libing.threadpool.TaskThread.run(TaskThread.java:16)

以上就是我的线程池0.1版,后续慢慢的完善!

posted @ 2012-09-24 21:20  softwa  阅读(114)  评论(0编辑  收藏  举报