多线程-一个线程池的简单实现

设计先行

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

在我们所要实现的 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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值