最近写了一个 http 代理服务器,发现访问网页时建立的连接很多,消耗的线程也非常的多,对于系统是
线程的建立的注销就占了绝大部分的cpu时间. 【程序编程相关:以前写的两个asp.net控件,超文本控】
一个不小的开销.而且这些线程存在的时间都很短,99%以上的线程存在的时间都在毫秒的等级,相对来说 【推荐阅读:生成随机字符串】
因此,在网上搜了一下线程池的资料,发现里面的东西不是太大太复杂,就是写得太烂,根本没有一点专业 【扩展信息:Resin的配置】水准.
没办法,只好自己简单的学习了一下 wait()/notify()机制,写了一个很小的线程池. 代码如下(一共2个类): /* *一个简单的线程池 threadpool .java */ public class threadpool { //以下是配置信息,可以更改 static int max_thread = 1000; //未使用 static int min_thread = 14; static int id = 1; //线程 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; } ... 下一页