解释“领导者/追随者”模式 [英] Explain "Leader/Follower" Pattern

查看:187
本文介绍了解释“领导者/追随者”模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到一个良好和可访问的领导者/关注者模式的解释。所有解释或者简单地在一些问题完全无意义



任何人都可以解释这种模式如何工作的原理,以及为什么以及如何提高性能超过更传统的异步IO模型?

解决方案

您可能已经阅读,该模式由4个组件组成:ThreadPool,HandleSet ,Handle,ConcreteEventHandler(实现EventHandler接口)。



你可以把它当成一个出租车站在晚上,所有的司机都睡觉,除了一个,的值。 ThreadPool是一个管理多个线程的工作站 - cab。



领导者正在等待HandleSet上的一个IO事件,就像驱动程序等待客户端一样。



当客户端(以识别IO事件的句柄的形式)到达时,领导者驱动器唤醒另一个驱动器作为下一个领导者并且服务于他的乘客的请求。 / p>

当他将客户端带到给定地址(调用ConcreteEventHandler并将Handle传递给它)时,下一个leader可以同时为另一个乘客服务。



当司机结束时,他把他的出租车带回车站,如果车站不是空的,就睡着了。



这种模式的优点是:




  • 不需要线程之间的通信,不需要同步,
    或共享内存(无锁,互斥体)

  • 更多ConcreteEventHandlers可以添加而不影响任何其他
    EventHandler

  • 最小化由于多个主题的延迟



是:




  • 复杂

  • 网络IO可能是瓶颈


I can't seem to find a good and accessible explanation of "Leader/Follower" pattern. All explanations either simply refer to it in the context of some problem, or are completely meaningless.

Can anyone explain to the the mechanics of how this pattern works, and why and how it improves performance over more traditional asynchronous IO models? Examples and links to diagrams are appreciated too.

解决方案

As you might have read, the pattern consists of 4 components: ThreadPool, HandleSet, Handle, ConcreteEventHandler (implements the EventHandler interface).

You can think of it as a taxi station at night, where all the drivers are sleeping except for one, the leader. The ThreadPool is a station managing many threads - cabs.

The leader is waiting for an IO event on the HandleSet, like how a driver waits for a client.

When a client arrives (in the form of a Handle identifying the IO event), the leader driver wakes up another driver to be the next leader and serves the request from his passenger.

While he is taking the client to the given address (calling ConcreteEventHandler and handing over Handle to it) the next leader can concurrently serve another passenger.

When a driver finishes he take his taxi back to the station and falls asleep if the station is not empty. Otherwise he become the leader.

The pros for this pattern are:

  • no communication between the threads are necessary, no synchronization, nor shared memory (no locks, mutexes) are needed.
  • more ConcreteEventHandlers can be added without affecting any other EventHandler
  • minimizes the latency because of the multiple threads

The cons are:

  • complex
  • network IO can be a bottleneck

这篇关于解释“领导者/追随者”模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆