解释“领导者/追随者"图案 [英] Explain "Leader/Follower" Pattern

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

问题描述

我似乎找不到对领导者/追随者"模式的良好且易于理解的解释.所有的解释都完全没有意义,就像 1.

I can't seem to find a good and accessible explanation of "Leader/Follower" pattern. All explanations are simply completely meaningless as in 1.

谁能向机制解释这种模式是如何工作的,以及它为什么以及如何比更传统的异步 IO 模型提高性能?示例和图表链接也值得赞赏.

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.

推荐答案

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

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

你可以把它想象成一个晚上的出租车站,那里所有的司机都在睡觉,只有一个,领导.ThreadPool 是一个管理许多线程的站 - cabs.

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.

leader 正在等待 HandleSet 上的 IO 事件,就像驱动程序等待客户端一样.

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

当客户端到达时(以标识 IO 事件的 Handle 的形式),领导者驱动程序唤醒另一个驱动程序成为下一个领导者,并为乘客的请求提供服务.

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.

当他将客户带到给定的地址时(调用 ConcreteEventHandler 并将 Handle 交给它),下一个领导者可以同时为另一位乘客服务.

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.

这种模式的优点是:

  • 线程之间不需要通信,不需要同步,也没有共享内存(没有锁,互斥锁)需要.
  • 可以添加更多 ConcreteEventHandlers 而不会影响任何其他事件处理程序
  • 由于多线程而最小化延迟

缺点是:

  • 复杂
  • 网络 IO 可能成为瓶颈

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

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