mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-15 08:17:06 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
1 parent
cb946c0cdc
commit
6cd2d0353a
219 files changed
+3308
-2819
No files matched your search
+19
-17
@@ -1,19 +1,21 @@
|
||||
---
|
||||
title: Leader/Followers
|
||||
title: "Leader-Followers Pattern in Java: Enhancing Efficiency with Dynamic Worker Allocation"
|
||||
shortTitle: Leader-Followers
|
||||
description: "Discover the Leader-Followers design pattern for efficient thread management and synchronization. Learn how to optimize resource usage and improve system performance with detailed examples and applications."
|
||||
category: Concurrency
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Performance
|
||||
- Synchronization
|
||||
- Thread management
|
||||
- Decoupling
|
||||
- Performance
|
||||
- Synchronization
|
||||
- Thread management
|
||||
---
|
||||
|
||||
## Intent
|
||||
## Intent of Leader/Followers Design Pattern
|
||||
|
||||
To manage a set of worker threads efficiently, where multiple threads take turns sharing a set of event sources in order to utilize fewer resources than one-thread-per-source.
|
||||
To efficiently manage a set of worker threads, the Leader-Followers pattern enables multiple threads to take turns sharing a set of event sources, optimizing resource utilization and improving performance compared to a one-thread-per-source approach.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Leader/Followers Pattern with Real-World Examples
|
||||
|
||||
Real-world Example
|
||||
|
||||
@@ -21,15 +23,15 @@ Real-world Example
|
||||
|
||||
In plain words
|
||||
|
||||
> The Leader and Followers design pattern utilizes a single leader thread to distribute work among multiple follower threads, effectively managing task delegation and thread synchronization to maximize resource efficiency.
|
||||
> The Leader-Followers design pattern utilizes a single leader thread to distribute work among multiple follower threads, effectively managing task delegation, thread synchronization, and improving resource efficiency in concurrent programming.
|
||||
|
||||
[martinfowler.com](https://martinfowler.com/articles/patterns-of-distributed-systems/leader-follower.html) says
|
||||
|
||||
> Select one server in the cluster as a leader. The leader is responsible for taking decisions on behalf of the entire cluster and propagating the decisions to all the other servers.
|
||||
|
||||
## Programmatic Example
|
||||
## Programmatic Example of Leader-Followers Pattern in Java
|
||||
|
||||
The Leader/Followers pattern is a concurrency pattern where one thread (the leader) waits for work to arrive, de-multiplexes, dispatches, and processes the work. Once the leader finishes processing the work, it promotes one of the follower threads to be the new leader. This pattern is useful for enhancing CPU cache affinity, minimizing locking overhead, and reducing event dispatching latency.
|
||||
The Leader-Followers pattern is a concurrency design pattern where one thread (the leader) waits for work to arrive, de-multiplexes, dispatches, and processes the work, thereby enhancing CPU cache affinity and reducing event dispatching latency. Once the leader finishes processing the work, it promotes one of the follower threads to be the new leader. This pattern is useful for enhancing CPU cache affinity, minimizing locking overhead, and reducing event dispatching latency.
|
||||
|
||||
In the provided code, we have a `WorkCenter` class that manages a group of `Worker` threads. One of these workers is designated as the leader and is responsible for receiving and processing tasks. Once a task is processed, the leader promotes a new leader from the remaining workers.
|
||||
|
||||
@@ -136,17 +138,17 @@ public class App {
|
||||
|
||||
This is a basic example of the Leader/Followers pattern. The leader worker processes tasks and promotes a new leader once it finishes a task. The new leader then starts processing the next task, and the cycle continues.
|
||||
|
||||
## Applicability
|
||||
## When to Use the Leader/Followers Pattern in Java
|
||||
|
||||
* Useful in scenarios where programs need to handle multiple services on a single thread to avoid resource thrashing and to improve scalability.
|
||||
* The Leader-Followers pattern is useful in scenarios requiring efficient handling of multiple services on a single thread, avoiding resource thrashing, and improving scalability in concurrent programming environments.
|
||||
* Applicable in server environments where multiple client requests must be handled concurrently with minimal resource consumption.
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of Leader-Followers Pattern in Java
|
||||
|
||||
* Network servers handling multiple incoming connections.
|
||||
* Event-driven applications that manage a large number of input/output sources.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Leader/Followers Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -158,12 +160,12 @@ Trade-offs:
|
||||
* Increased complexity in managing the synchronization between leader and followers.
|
||||
* Potential for underutilization of resources if not correctly implemented.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Half-Sync/Half-Async](https://java-design-patterns.com/patterns/half-sync-half-async/): Leader and Followers can be seen as a variation where the synchronization aspect is divided between the leader (synchronous handling) and followers (waiting asynchronously).
|
||||
* [Thread Pool](https://java-design-patterns.com/patterns/thread-pool/): Both patterns manage a pool of worker threads, but Thread Pool assigns tasks to any available thread rather than using a leader to distribute work.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Java Concurrency in Practice](https://amzn.to/4aRMruW)
|
||||
* [Pattern-Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects](https://amzn.to/3UgC24V)
|
||||
Reference in new issue
Block a user