Java线程池

最近比较无聊,也是快过年了,在家的效率比较低下,两天时间才把这个线程池写出来,基本上没有什么新的知识,只是为了防止手生,也正好是为蓝桥杯软件设计大赛热身,就写了这个比较简单的线程池。

还是有一些小的bug的,比如如果退出程序时,还有工作线程,那么就会造成死锁。还有,线程的最大数量我没有设置上限,如果短时间内有大量的线程产生,效果就像没有线程池一样,标准做法应该是在线程数量达到最大值的时候不再建立新的线程,而是开始不断地分发任务。所以,这些bug还是读者慢慢体会吧。以下是程序:

先是线程池的:

package 线程池1;

import java.util.List;
import java.util.Vector;

public class ThreadPool {
	private static ThreadPool Instance = null;
	//定义优先级别,分别放在三个vector中
	public final static int LOW_PRIORITY = 0;
	public final static int NORMAL_PRIORITY = 1;
	public final static int HIGH_PRIORITY = 2;
	private final static int THREAD_NUM = 2;
	//保存空余线程的List
	private List[] IdleThreads;
	private boolean isShutDown = false;
	private int ThreadNUM;	//创建的线程数量
	private boolean isDebug = false;
	
	//singleton模式,所以构造函数为私有
	private ThreadPool(){
		List[] idleThreads = {new Vector(THREAD_NUM), new Vector<>(THREAD_NUM), new Vector<>(THREAD_NUM)};
		IdleThreads = idleThreads;
		ThreadNUM = 0;
	}
	
	/**
	 * @return 线程的数量
	 */
	public int  getThreadNUM() {
		return ThreadNUM;
	}
	
	/**
	 * @return 返回一个线程池类的实例
	 */
	public static ThreadPool instance() {
		if (Instance == null) {
			Instance = new ThreadPool();
			System.out.println("ThreadPool:NULL!!!");
		}
		return Instance;
	}
	
	/**
	 * @return  是否输出调试信息
	 */
	public boolean isDebug() {
		return isDebug;
	}
	
	protected synchronized void repool(PooledThread _repoolingThread){
		if (!isShutDown) {
			if (isDebug) {
				System.out.println("ThreadPool.repool():repooling...");
			}
			switch (_repoolingThread.getPriority()) {
			case Thread.MIN_PRIORITY:
				IdleThreads[LOW_PRIORITY].add(_repoolingThread);
				break;
	
			case Thread.NORM_PRIORITY:
				IdleThreads[NORMAL_PRIORITY].add(_repoolingThread);
				break;
				
			case Thread.MAX_PRIORITY:
				IdleThreads[HIGH_PRIORITY].add(_repoolingThread);
				break;
				
			default:
				throw new IllegalStateException("Illegal Priority found while repooling a thread;");
			}
			notifyAll(); //通知所有线程
		}else {
			if (isDebug) {
				System.out.println("ThreadPool.repool():Destroying incoming thread...");
			}
			_repoolingThread.shutDown();
			if (isDebug) {
				System.out.println("ThreadPool.recycle():Done.");
			}
		}
	}
	
	/**
	 * @param _debug是否输出调试信息
	 */
	public void setDebug(boolean _debug) {
		isDebug = _debug;
	}
	
	public synchronized void shutDown() {
		isShutDown = true;
		if (isDebug) {
			System.out.println("ThreadPool:Shutting down!...");
		}
		for (int Proindex = 0; Proindex <= HIGH_PRIORITY; Proindex++) {
			List proThreads = IdleThreads[Proindex];
			System.out.println(proThreads.size());
			for (int Threadindex = 0; Threadindex < proThreads.size(); Threadindex++) {
				PooledThread idleThread = (PooledThread) proThreads.get(Threadindex);
				idleThread.shutDown();
			}
		}
		notifyAll();
		if (isDebug) {
			System.out.println("ThreadPool:ShutDown Done!");
		}
	}
	
	public synchronized void start(Runnable _targetRunnable, int _priority) {
		PooledThread thread = null;
		List idleList = IdleThreads[_priority];
		if (idleList.size() > 0) {
			int lastIndex = idleList.size() - 1;
			thread = (PooledThread)idleList.get(lastIndex);
			idleList.remove(lastIndex);
			System.out.println("ThreadPool:remove " + lastIndex);
			thread.setTarget(_targetRunnable);
		}else {
			System.out.println("ADD");
			ThreadNUM++;
			thread = new PooledThread(_targetRunnable, "PooledThread#" + ThreadNUM, this);
			switch (_priority) {
			case LOW_PRIORITY:
				thread.setPriority(Thread.MIN_PRIORITY);
				if (isDebug) {
					System.out.println("ThreadPool: add thread Low_Priority");
				}
				break;
				
			case NORMAL_PRIORITY:
				thread.setPriority(Thread.NORM_PRIORITY);
				if (isDebug) {
					System.out.println("ThreadPool: add thread Normal_Priority");
				}
				break;
				
			case HIGH_PRIORITY:
				thread.setPriority(Thread.MAX_PRIORITY);
				if (isDebug) {
					System.out.println("ThreadPool: add thread High_Priority");
				}
				break;
				
			default:
				thread.setPriority(Thread.NORM_PRIORITY);
				if (isDebug) {
					System.out.println("ThreadPool: add thread Normal_Priority");
				}
				break;
			}
			thread.start();
			if (isDebug) {
				System.out.println("ThreadPool: Thread Start;");
			}
		}
	}
}

然后是工作线程的:

package 线程池1;

public class PooledThread extends Thread{
    private ThreadPool threadPool;
    private Runnable targetRunnable;
    private boolean isShutdown = false;
    private boolean isidle = false;
   
    private PooledThread(){
        super();
    }
   
    private PooledThread(Runnable _targetRunnable){
        super(_targetRunnable);
    }
   
    /**
     * @param _targetRunnable 一个线程任务
     * @param _nameString名称
     * @param _threadPool线程池实例
     */
    public PooledThread(Runnable _targetRunnable, String _nameString, ThreadPool _threadPool) {
        super(_nameString);
        threadPool = _threadPool;
        targetRunnable = _targetRunnable;
    }
   
    private PooledThread(String _nameString) {
        super(_nameString);
    }
   
    private PooledThread(ThreadGroup _group, Runnable _targetRunnable){
        super(_group, _targetRunnable);
    }
   
    private PooledThread(ThreadGroup _group, Runnable _targetRunnable, String _nameString){
        super(_group, _targetRunnable, _nameString);
    }
   
    private  PooledThread(ThreadGroup _group, String _name) {
        super(_group, _name);
    }
   
    /**
     * @return 返回当前状态
     */
    public boolean isIdled() {
        return isidle;
    }
   
    public void run() {
        while (!isShutdown) {
            isidle = false;
            if (targetRunnable != null) {
                targetRunnable.run();    //运行runnable中的代码
            }
            isidle = true;
            try {
                threadPool.repool(this);
                synchronized (this) {
                    wait();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            isidle = false;
        }
        System.out.println(this.getName() + ":I am died!");
        //循环结束,但是不能让他死亡,否线程池的作用就等于没用
    }
   
    /**
     * @param _newRunnable设置一个新的任务
     */
    public synchronized void setTarget(Runnable _newRunnable) {
        targetRunnable = _newRunnable;
        notifyAll();    //唤醒睡眠的线程
    }
   
    /**
     * @author mikecoder
     * @描述 将这个线程关闭
     */
    public synchronized void shutDown() {
        isShutdown = true;
        notifyAll();
    }
}

最后的就是这个线程池的测试代码:

package 线程池1;


public class Main {
	/**
	 * @param _args 我也不知道用来干什么
	 * @throws InterruptedException 
	 */
	public static void main(String[] _args) throws InterruptedException {
		class TaskRunnable1 implements Runnable{
			public void run() {
				System.out.println("Sleeping 5 sec");
				synchronized (this) {
					try {
						wait(5000);
						System.out.println("Hello World!");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				System.out.println("Leaving!...");
			}
		}
		class TaskRunnable2 implements Runnable{
			@Override
			public void run() {
				System.out.println("Sleeping 5 sec");
				synchronized (this) {
					try {
						wait(5000);
						System.out.println("Hello World Fuck!");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				System.out.println("Leaving!...");
			}
		}
		System.out.println("ThreadPool Test!...");
		ThreadPool threadPool = ThreadPool.instance();
		threadPool.setDebug(true);
		TaskRunnable1 runnable = new TaskRunnable1();
		TaskRunnable2 runnable2 = new TaskRunnable2();
		for (int i = 0; i < 4; i++) {
			Thread.sleep(6000);
			threadPool.start(runnable, ThreadPool.HIGH_PRIORITY);
			System.out.println(threadPool.getThreadNUM());
		}
		Thread.sleep(10000);
		threadPool.start(runnable2, ThreadPool.HIGH_PRIORITY);
		Thread.sleep(10000);
		threadPool.shutDown();
	}
}

That’s all.

Leave a Reply

Your email address will not be published. Required fields are marked *