4

【从0到1学习Java线程池】一个Java线程池的简单实现

 2 years ago
source link: http://blog.luoyuanhang.com/2017/03/15/thread-pool-in-java-3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【从0到1学习Java线程池】一个Java线程池的简单实现

发表于 Mar 15 2017

| 0 comments

| 阅读次数

这是【从0到1学习Java线程池】系列文章的第 叁 篇,该系列文章总共三篇,介绍了 Java 线程池的使用以及原理,并且最后会实现一个基本的线程池。本篇文章实现了一个简单的 Java 线程池。

【从0到1学习Java线程池】系列文章共有3篇,目录如下:

从上两篇文章中,我们已经知道了线程池的基本原理,这篇文章我们就来具体实现一个简单的 Java 线程池。

想要实现一个线程池,我们首先要来进行设计,考虑它需要有哪些功能,如何设计和安排这些功能是至关重要的。

在我们所要实现的 Java 线程池需要有:

  • 任务队列:它能够添加或者删除任务,并且它还需要支持原子操作,不能同时有多个线程从中取出任务。
  • 通知机制:如果任务队列为空,工作线程将会阻塞在获取任务这一操作上;如果这时任务队列中有了新的任务,需要通知工作线程从中获取任务来执行。
  • 线程类:线程类的例程是用来获取任务和执行任务的。
  • 任务类:用于被线程抓取和执行的任务。
  • 线程管理类:能够创建一定数量的线程,并且提供对任务队列进行操作的方法(获取任务、添加任务等)。

系统配置类

其中的参数主要是该线程池所支持的最大线程数

public class SystemConfig {
static final int THREAD_POOL_MAX_SIZE = 20;

public static int getThreadDefalutSize(){
return THREAD_POOL_MAX_SIZE;
}
}
public class Task implements Runnable {
@Override
public void run() {

}
}

线程管理类

public class ThreadPoolManager extends ThreadGroup {
int isThreadPoolValid = 0;

int sizeOfPoolThread = SystemConfig.getThreadDefalutSize();

List<Task> taskList= new LinkedList<Task>();


public ThreadPoolManager(String threadpoolname) {
super(threadpoolname);
setDaemon(true);
}

public synchronized void startThreadPool(){
if(sizeOfPoolThread == 0 || isThreadPoolValid != 0){
try{
throw new Exception();
}
catch(Exception exception){
exception.printStackTrace();
}
return;
}
if(taskList == null){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
return;
}

for(int i = 0; i < sizeOfPoolThread; i++){
new WorkThread(i).start();
}

isThreadPoolValid = 1;
}

public synchronized void stopThreadPool(){
if(sizeOfPoolThread == 0 || isThreadPoolValid != 0){
try{
throw new Exception();
}
catch(Exception exception){
exception.printStackTrace();
}
return;
}
if(taskList == null){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
return;
}

taskList.clear();
sizeOfPoolThread = 0;
isThreadPoolValid = 0;
interrupt();
}

public synchronized void addTask(Task newTask){
if(taskList == null){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
return;
}

taskList.add(newTask);

notify();
}

public synchronized Task getTask(){
if(taskList == null){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

while(taskList.size() == 0){
try{
wait();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
return taskList.remove(0);

}

private class WorkThread extends Thread{
public WorkThread(int threadID){
super(ThreadPoolManager.this, ""+threadID);
}

public void run(){
while(!isInterrupted()){
Task runTask = getTask();

if(runTask == null)
break;
runTask.run();
}
}
}
}

测试任务

public class TestTask extends Task {
private int i;

public TestTask(int i){
this.i = i;
}

public void run(){
System.out.println("Task " + i + " is RUNNING.");
}
}

主程序

public class ThreadPoolTest {
public static void main(String[] args) {
ThreadPoolManager manager = new ThreadPoolManager("SimplePool");
manager.startThreadPool();

for(int i = 0; i < 5; i++){
Task task = new TestTask(i);
manager.addTask(task);
}
}
}
Task 3 is RUNNING.
Task 4 is RUNNING.
Task 1 is RUNNING.
Task 0 is RUNNING.
Task 2 is RUNNING.

本文的版权归作者 罗远航 所有,采用 Attribution-NonCommercial 3.0 License。任何人可以进行转载、分享,但不可在未经允许的情况下用于商业用途;转载请注明出处。感谢配合!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK