您现在的位置是:首页 >

线程池满了 一个非常简单和短小的线程池

火烧 2022-04-24 00:13:26 1032
一个非常简单和短小的线程池 最近写了一个 HTTP 代理服务器 发现访问网页时建立的连接很多 消耗的线程也非常的多 对于系统是一个不小的开销 而且这些线程存在的时间都很短 %以上的线程存在的时间都在毫

一个非常简单和短小的线程池  

    最近写了一个 HTTP 代理服务器 发现访问网页时建立的连接很多 消耗的线程也非常的多 对于系统是一个不小的开销 而且这些线程存在的时间都很短 %以上的线程存在的时间都在毫秒的等级 相对来说线程的建立的注销就占了绝大部分的CPU时间

    一个很小的线程池     代码如下(一共 个类):    /*    *一个简单的线程池 ThreadPool java    */    public class ThreadPool {    //以下是配置信息 可以更改    static int MAX_THREAD = ; //未使用    static int MIN_THREAD = ;     static int id = ; //线程 ID 号 主要用于监视线程的工作情况     static private ThreadPool pool = new ThreadPool();    static public ThreadPool getThreadPool() {    return pool;    }     Stack<WorkThread> stack = new Stack<WorkThread>(MIN_THREAD);    private ThreadPool() {    }     synchronized public boolean putWorkThread(WorkThread wt) {    if(stack size()<MIN_THREAD){    stack push(wt);    return true;    } else {    return false;    }    }     synchronized public WorkThread getWorkThread() {    WorkThread wt = null;    if(stack isEmpty()) {    wt = new WorkThread(this);    new Thread(wt 线程ID: +id) start();    id++;    } else {    wt = stack pop();    }    return wt;    }    }         /*    *工作线程类 WorkThread java    */    public class WorkThread implements Runnable {    Object lock = new Object();    Runnable runner = null;    ThreadPool pool = null;     public WorkThread(ThreadPool pool) {    this pool = pool;    }     public void start(Runnable r) {    runner = r;    synchronized(lock) {    lock notify();    }    }     public void run() {    while(true) {    if(runner != null) {    runner run();    runner = null; //及时回收资源    }    if(pool putWorkThread(this)) {    System out println (Thread currentThread() getName()+ 被回收! );    synchronized(lock) {    try {    lock wait();    } catch (InterruptedException ie) {    System out println ( 停止线程时出现异常 );    }    }    } else {    System out println (Thread currentThread() getName()+ 被丢弃! );    break;    }    }    }    }

    使用方法:    Runnable r = ;    new Thread(r) start();    这是你以前的代码 使用线程池只需要将后一句变成    ThreadPool getThreadPool() getWorkThread() start(r);    就可以了 其他的代码不用任何更改

线程池满了 一个非常简单和短小的线程池
lishixinzhi/Article/program/Java/gj/201311/27725  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码