线程池学习研究-(自实现)2
在自实现1中写了0.1版本,基本上只是完成了线程池需要的三个基本要素,任务队列,工作线程,线程池管理器
今天在上一个版本的基础上改了下:姑且算作1.0版,基本上能跑通测试,接下来还需要进一步优化,调整
他们之间的关系这里画个简单的图:
Task:需要完成的任务,一般是一个接口,我们的任务区实现该接口
线程管理器:用于创建,销毁线程,主要管理一个任务队列
TaskThread:用于真正去执行任务的线程
下面是他们的代码:
Task:
View Code
public class Task { //索引为了测试打印用 private int index; public Task(int index) { this.index = index; } public void doTask() { System.out.println("do the task"); } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }
线程管理器:
View Code
import java.util.LinkedList; public class MyThreadPool { private LinkedList<Task> taskList; private TaskThread[] taskThreads; // 默认工作线程数为5 private static int work_num = 5; private static int work_count = 0; /** * 初始化,默认为5个工作线程 */ public MyThreadPool() { taskThreads = new TaskThread[this.work_num]; taskList = new LinkedList<Task>(); for (int i = 0; i < 5; i++) { TaskThread taskThread = new TaskThread(taskList, i); taskThreads[i] = taskThread; } System.out.println("taskThreads的长度为 :" + taskThreads.length); } /** * 带参数的初始化 * * @param coreSize */ public MyThreadPool(int coreSize) { this.work_num = coreSize; taskThreads = new TaskThread[this.work_num]; taskList = new LinkedList<Task>(); for (int i = 0; i < this.work_num; i++) { TaskThread taskThread = new TaskThread(taskList, i); taskThreads[i] = taskThread; } System.out.println("taskThreads的长度为 :" + taskThreads.length); } /** * 添加任务 * * @param task */ public void addTask(Task task) { synchronized (taskList) { taskList.add(task); taskList.notifyAll(); } } public void destory() { for (int i = 0; i < this.work_num; i++) { taskThreads[i].stopThread(); taskThreads[i] = null; } } public boolean isEmpty() { return taskList.isEmpty(); } }
TaskThread:
View Code
import java.util.LinkedList; public class TaskThread extends Thread { private int index = -1; private boolean isRunning = true; private boolean isWaiting = false; private LinkedList<Task> taskList; public TaskThread(LinkedList<Task> taskList, int index) { this.index = index; this.taskList = taskList; this.start(); } public void stopThread() { this.isRunning = false; } public boolean isWaiting() { return isWaiting; } public void run() { while (isRunning) { Task task = null; while (isRunning && taskList.isEmpty()) { synchronized (taskList) { try { taskList.wait(20); System.out.println("线程等待"); } catch (InterruptedException e) { e.printStackTrace(); } } } // 取得一个任务 if (isRunning) { task = taskList.poll(); } if (null != task) { this.isWaiting = false; System.out.println("线程" + this.index + ":执行任务 task"+task.getIndex()+"开始"); task.doTask(); this.isWaiting = true; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }
Test:
View Code
public class Test { public static void main(String[] args) { MyThreadPool pool = new MyThreadPool(3); for (int i=0;i<15;i++) { Task task = new Task(i); pool.addTask(task); System.out.println("添加任务 task"+i); } try { Thread.sleep(6000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } pool.destory(); } }
测试结果如下:
View Code
taskThreads的长度为 :3 添加任务 task0 添加任务 task1 添加任务 task2 添加任务 task3 添加任务 task4 添加任务 task5 添加任务 task6 添加任务 task7 添加任务 task8 添加任务 task9 添加任务 task10 添加任务 task11 添加任务 task12 添加任务 task13 线程0:执行任务 task0开始 添加任务 task14 do the task 线程2:执行任务 task1开始 do the task 线程1:执行任务 task2开始 do the task 线程1:执行任务 task3开始 do the task 线程0:执行任务 task4开始 do the task 线程2:执行任务 task5开始 do the task 线程1:执行任务 task6开始 do the task 线程0:执行任务 task7开始 do the task 线程2:执行任务 task8开始 do the task 线程1:执行任务 task9开始 do the task 线程0:执行任务 task10开始 do the task 线程2:执行任务 task11开始 do the task 线程1:执行任务 task12开始 do the task 线程2:执行任务 task13开始 do the task 线程0:执行任务 task14开始 do the task 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待 线程等待
这样1.0版算基本完成了,接下来就是进行改进和优化,还有很多需要修改的地方!
一点点进步!