美文网首页
简易线程池实现

简易线程池实现

作者: d1ab10 | 来源:发表于2016-12-30 11:15 被阅读30次

    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);
        }
    }
    

    测试结果:



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

    相关文章

      网友评论

          本文标题:简易线程池实现

          本文链接:https://www.haomeiwen.com/subject/lqlmvttx.html