【ICE】【08】动态线程池

1.为了降低线程间上下文切换的开销,ice使用了leader/follower设计模式

如果以下连接不可以,可以百度搜索
Leader/Followers: A Design Pattern for Efficient Multi-Threaded Event Demultiplexing and Dispatching

https://www.researchgate.net/publication/2647023_LeaderFollowers_-_A_Design_Pattern_for_Efficient_Multi-threaded_Event_Demultiplexing_and_Dispatching

【ICE】【08】动态线程池_第1张图片
所有的线程有三种状态:leader follower processing
永远只有一个leader,所有的follower线程在等待成为leader
当应用程序启动的时候产生一个leader线程,这个线程等待网络IO事件,当线程接收到网络IO事件时,leader线程通知下一个follower线程成为leader,自己变成processing状态,处理完事件后变成follower队列中的一个线程等待下一次成为leader

感觉有点像一群小朋友滑滑梯游戏
等待下一个滑的时leader
leader之后的是一个follower对列
正在滑的是processing线程,当他滑完后进入follower的队尾

2.Dynamic Thread Pools

A dynamic thread pool can grow and shrink when necessary in response to changes in an application’s work load. All thread pools have at
least one thread, but a dynamic thread pool can grow as the demand for threads increases, up to the pool’s maximum size. Threads may
also be terminated automatically when they have been idle for some time.
The dynamic nature of a thread pool is determined by the configuration properties name.Size, name.SizeMax, and name.ThreadIdleTi
me. A thread pool is not dynamic in its default configuration because name.Size and name.SizeMax are both set to 1, meaning the pool
can never grow to contain more than a single thread. To configure a dynamic thread pool, you must set at least one of name.Size or name.
SizeMax to a value greater than 1. We can use several configuration scenarios to explore the semantics of dynamic thread pools in greater
detail:
name.SizeMax=5
This thread pool initially contains a single thread because name.Size has a default value of 1, and Ice can grow the pool up to the
maximum of 5 threads. During periods of inactivity, idle threads terminate after 60 seconds (the default value for name.ThreadIdl
eTime) until the pool contains just 1 thread again.
name.Size=3
name.SizeMax=5
This thread pool starts with 3 active threads but otherwise behaves the same as in the previous configuration. The pool can still
shrink to a size of 1 as threads become idle.
name.Size=3
name.ThreadIdleTime=10
This thread pool starts with 3 active threads and shrinks quickly to 1 thread during periods of inactivity. As demand increases again,
the thread pool can return to its maximum size of 3 threads (name.SizeMax defaults to the value of name.Size).
name.SizeMax=5
name.ThreadIdleTime=0
This thread pool can grow from its initial size of 1 thread to contain up to 5 threads, but it will never shrink because name.ThreadI
dleTime is set to 0.
name.Size=5
name.ThreadIdleTime=0
This thread pool starts with 5 threads and can neither grow nor shrink.
To summarize, the value of name.ThreadIdleTime determines whether (and how quickly) a thread pool can shrink to a size of 1. A thread
pool that shrinks can also grow to its maximum size. Finally, setting name.SizeMax to a value larger than name.Size allows a thread pool
to grow beyond its initial capacity.

动态线程池

动态线程池可以根据应用程序工作负载的变化在必要时增长和收缩。
所有线程池的至少有一个线程,但是动态线程池可以随着线程需求的增加而增长,直到池的最大大小。线程可能当它们闲置一段时间后也会自动终止。
线程池的动态特性由配置属性决定name.Size, name.SizeMax, and name.ThreadIdleTime。
线程池在其默认配置中不是动态的,因为名称、大小以及名称.SizeMax都设置为1,表示池不能增长到包含超过一个线程。
要配置动态线程池,必须至少设置一个名称、大小或者名字。
SizeMax设置为大于1的值。我们可以使用几个配置场景来研究更大范围内的动态线程池的语义

详细信息:

name.SizeMax=5
此线程池最初包含一个线程,因为名称、大小默认值为1,并且Ice可以将池增大到最多5个。在非活动期间,空闲线程在60秒后终止(默认值为name.ThreadIdleTime)直到池再次包含1个线程。

name.Size=3
name.SizeMax=5

此线程池从3个活动线程开始,但在其他方面的行为与前一个配置中的相同。游泳池还能当线程空闲时收缩到1的大小。

name.Size=3
name.ThreadIdleTime=10

此线程池从3个活动线程开始,在不活动期间迅速缩小为1个线程。随着需求再次增加,线程池可以返回到其最大大小3个线程(名称.SizeMax默认值为名称、大小).

name.SizeMax=5
name.ThreadIdleTime=0

这个线程池可以从最初的1个线程增长到最多包含5个线程,但它永远不会收缩,因为name.ThreadIdleTime设置为0。

name.Size=5
name.ThreadIdleTime=0
这个线程池从5个线程开始,既不能增长也不能收缩。

总而言之,name.ThreadIdleTime得到值确定线程池是否(以及以多快的速度)收缩到1的大小。一根线

收缩的池也可以增长到最大大小。最后,设置名称.SizeMax值大于名称、大小允许线程池超出最初的能力。

你可能感兴趣的:(ice)