浅谈“反应堆模式”及其应用

反应堆模式是在 维基百科中解释的,它有点太抽象了。你能更具体地描述一下这种模式吗?理想情况下,可以使用代码片段或描述某些反应器模式应用程序的高级类图。

43890 次浏览

You might want to check the original paper describing it http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf

The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. Each service in an application may consist of serveral methods and is represented by a separate event handler that is responsible for dispatching service-specific requests. Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. Demultiplexing of service requests is performed by a synchronous event demultiplexer.

A reactor allows multiple tasks which block (say due to IO) to be processed efficiently using a single thread. The reactor manages a pool of handlers and runs an event loop. When it is called to perform a task it links it with a new or vacant handler making it active. The event loop (1) finds all the handlers that are active and unblocked (or delegates this to a dispatcher implementation) (2) executes each of these found handlers sequentially until they either complete or reach a point where they block. Completed handlers become inactive and vacant for reuse whereas blocked active handlers yield, allowing the event loop to continue. (3) Repeats from step (1)