java线程池

 

帅气的ThreadPoolExecutor

http://blog.csdn.net/cutesource/article/details/6061229

http://wenku.baidu.com/view/8f4fc14ffe4733687e21aa55.html

http://www.cnblogs.com/scottyao/archive/2010/03/15/1686512.html

http://www.2cto.com/kf/201105/91810.html

http://www.ibm.com/developerworks/cn/java/j-jtp0730/

 

和seda的关系,伸缩的线程池,固定的消息队列,拒绝处理器

http://blog.sina.com.cn/s/blog_4bc179a80100xko5.html

http://blog.csdn.net/historyasamirror/article/details/5961368

进行了小实验,如果是计算密集型的,无阻塞的,那么使用单线程效率佳(对于单核而言)。

 

For compute‐intensive tasks, an Ncpu‐processor system usually achieves optimum utilization with a thread pool of Ncpu +1 threads. (Even compute‐intensive threads occasionally take a page fault or pause for some other reason, so an "extra" runnable thread prevents CPU cycles from going unused when this happens.)

 

如果有阻塞的,那么使用多线程,而线程池的数量需要权衡。

原来多线程是为阻塞式任务而准备的,充分利用cpu时间片。如果任务不阻塞、且为单核,那么利用多线程就没意义了。

 

If you have enough threads to keep all the CPUs busy, creating more threads won't help and may even hurt. 

 

对于具有N个处理器的系统,需要设置大约 N*(1+WT/ST) 个线程来保持处理器得到充分利用。

实验记录:(发现ibatis的读取缓存?????)

任务100ms计算+500ms线程沉睡,运行1000ms,双核,理论最优值是12个线程数,并且最多不会超过12个任务。

和理论值类似,可以通过类似甘特图来演绎多线程过程。先用单核演算,然后*N,可以演绎出吞吐量,在单位时间内处理的任务数

 

java线程池_第1张图片

 

 

 

线程数量1,运行1个

线程数量2,运行2个

 

线程数量4,运行4个

 

 

线程数量8,运行8个

 

线程数量12,运行12个

使用有限尺寸线程池需要注意一点,如果任务是阻塞式任务需要调用阻塞超时时间版本,防止线程池被阻塞任务耗尽,导致其他线程出现饥渴。

 

If a thread pool is too big, then threads compete for scarce CPU and memory resources, resulting in higher 

memory usage and possible resource exhaustion. If it is too small, throughput suffers as processors go unused despite 

available work。

 

If you have different categories of tasks with very different behaviors, consider using multiple thread pools so each can be tuned according to its workload.-》》》》》SEDA

关于计算线程数量的补充,需要同时考虑其他资源

 

Of course, CPU cycles are not the only resource you might want to manage using thread pools. Other resources that can 

contribute to sizing constraints are memory, file handles, socket handles, and database connections. Calculating pool 

size constraints for these types of resources is easier: just add up how much of that resource each task requires and 

divide that into the total quantity available. The result will be an upper bound on the pool size。

ThreadPoolExecutor可以通过限制线程数量、队列长度和饱和策略来实现seda中的线程池。

ThreadPoolExecutor扩展点

 

ThreadPoolExecutor was designed for extension, providing several "hooks" for subclasses to overridebeforeExecute, 

afterExecute, and terminatethat can be used to extend the behavior of ThreadPoolExecutor. 

你可能感兴趣的:(多线程)