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

简易线程池实现

作者: 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);
    }
}

测试结果:



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

相关文章

  • 线程池-1

    要点: 1.线程池原理,wait/notify ? 实现一个简易的线程池,http://ifeve.com/thr...

  • 简易线程池实现

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

  • 简易线程池

    来自《Java并发编程的艺术》 一个简易的线程池的实现。三个类:ThreadPool线程池接口DefaultThr...

  • 线程池简易实现和线程池源码

    线程池简单实现 源码 ThreadPoolExecutor 使用 int 的高 3 位来表示线程池状态,低 29 ...

  • 简易线程池的实现

    构成线程池的基本元素 线程池中的线程 任务队列 生产者 消费者 线程池 消费者 生产者 问题 任务队列的大小:如果...

  • Android HttpURLConnection简易框架

    1、概述 封装一个简易的HttpURLConnection简易框架,内部通过线程池来进行网络请求。同时实现了请求返...

  • 线程以及java线程池实现分享

    线程以及java线程池实现分享 线程简介 JDK线程池的工作原理 JDK线程池的实现细节 1.线程简介-由来 1....

  • java多线程面试题

    实现多线程的方法 1.实现Thread接口 2.实现Runnable接口创建线程 3.实现 线程池 创建线程池的代...

  • 不怕难之线程池原理

    一、线程池状态 ThreadPoolExecutor 是 JDK 中的线程池实现,这个类实现了一个线程池需要的各个...

  • 线程池的原理

    参考 深入Java源码理解线程池原理 线程池是对CPU利用的优化手段 线程池使用池化技术实现,替他的实现还有连接池...

网友评论

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

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