简易线程池实现

2016-12-30  本文已影响30人  d1ab10

ThreadPool.java

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

public class ThreadPool {
    private static ThreadPool instance=null;
    private List<PThread> idleThreads;
    private int threadCounter;
    private boolean isShutDown=false;
    
    private ThreadPool(){
        this.idleThreads=new Vector(5);
        threadCounter=0;
    }
    public int getCreatedThreadsCount() {
        return threadCounter;
    }
    public synchronized static ThreadPool getInstance() {
        if(instance==null)
            instance=new ThreadPool();
        return instance;
    }
    protected synchronized void repool(PThread repoolingThread) {
        if(!isShutDown)
            idleThreads.add(repoolingThread);
        else
            repoolingThread.shutdown();
    }
    public synchronized void shutdown() {
        isShutDown=true;
        for(int threadIndex=0;threadIndex<idleThreads.size();threadIndex++){
            PThread idleThread=(PThread)idleThreads.get(threadIndex);
            idleThread.shutdown();
        }
    }
    public synchronized void start(Runnable target) {
        PThread thread=null;
        if(idleThreads.size()>0){
            int lastIndex=idleThreads.size()-1;
            thread=(PThread)idleThreads.get(lastIndex);
            idleThreads.remove(lastIndex);
            thread.setTarget(target);
        }
        else {
            threadCounter++;
            thread=new PThread(target, "PThread #"+threadCounter, this);
            thread.start();
        }
    }
}

PThread.java--永不停止的线程,用于配合线程池

package javaTest;

public class PThread extends Thread{
    private ThreadPool pool;
    private Runnable target;
    private boolean isShutDown=false;
    private boolean isIdle=false;
    public PThread(Runnable target,String name,ThreadPool pool){
        super(name);
        this.pool=pool;
        this.target=target;
    }
    public Runnable getTarget() {
        return target;
    }
    public boolean isIdle() {
        return isIdle;
    }
    public synchronized void setTarget(java.lang.Runnable newTarget) {
        target=newTarget;
        notifyAll();
    }
    public synchronized void shutdown() {
        isShutDown=true;
        notifyAll();
    }
    public void run() {
        while (!isShutDown) {
            isIdle=false;
            if(target!=null){
                target.run();
            }
            isIdle=true;
            try{
                pool.repool(this);
                synchronized (this) {
                    wait();
                }
            }
            catch(InterruptedException iException){
            }
            isIdle=false;
        }
    }
}

Mythread.java--需要用来执行的任务

public class Mythread implements Runnable {
    protected String name;
    public Mythread () {    
    }
    public Mythread (String name) {
        this.name=name;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(100);//任务内容
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
    }
}

Tester.java--测试类

import java.util.Date;

public class Tester {
    public static void main(String args[]) {
        double formerTime,latterTime;
        formerTime=System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            new Thread(new Mythread("testNoThreadPool"+Integer.toString(i))).start();
        }
        latterTime=System.currentTimeMillis()-formerTime;
        System.out.println("NoThreadPool测试完成!"+latterTime);
        formerTime=System.currentTimeMillis();
        for(int i=0;i<10000;i++)
            ThreadPool.getInstance().start(new Mythread("testThreadPool"+Integer.toString(i)));
        latterTime=System.currentTimeMillis()-formerTime;
        System.out.println("ThreadPool测试完成!"+latterTime);
    }
}

测试结果:



可见,使用线程池管理线程的策略,在这种情况下对性能的优化还是很可观的。

上一篇 下一篇

猜你喜欢

热点阅读