Skip to content

Commit 47d1b88

Browse files
Merge branch 'iluwatar:master' into translate-to-persian
2 parents ba5872a + bf16400 commit 47d1b88

File tree

33 files changed

+1817
-32
lines changed

33 files changed

+1817
-32
lines changed

.all-contributorsrc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,6 +3493,42 @@
34933493
"contributions": [
34943494
"code"
34953495
]
3496+
},
3497+
{
3498+
"login": "ssrijan-007-sys",
3499+
"name": "ssrijan-007-sys",
3500+
"avatar_url": "https://avatars.githubusercontent.com/u/137605821?v=4",
3501+
"profile": "https://github.com/ssrijan-007-sys",
3502+
"contributions": [
3503+
"code"
3504+
]
3505+
},
3506+
{
3507+
"login": "e5LA",
3508+
"name": "e5LA",
3509+
"avatar_url": "https://avatars.githubusercontent.com/u/208197507?v=4",
3510+
"profile": "https://github.com/e5LA",
3511+
"contributions": [
3512+
"code"
3513+
]
3514+
},
3515+
{
3516+
"login": "maziyar-gerami",
3517+
"name": "Maziyar Gerami",
3518+
"avatar_url": "https://avatars.githubusercontent.com/u/122622721?v=4",
3519+
"profile": "http://maziyar-gerami.github.io/portfolio/",
3520+
"contributions": [
3521+
"translation"
3522+
]
3523+
},
3524+
{
3525+
"login": "yybmion",
3526+
"name": "yoobin_mion",
3527+
"avatar_url": "https://avatars.githubusercontent.com/u/113106136?v=4",
3528+
"profile": "https://github.com/yybmion",
3529+
"contributions": [
3530+
"code"
3531+
]
34963532
}
34973533
],
34983534
"contributorsPerLine": 6,

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
77
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
88
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
9-
[![All Contributors](https://img.shields.io/badge/all_contributors-383-orange.svg?style=flat-square)](#contributors-)
9+
[![All Contributors](https://img.shields.io/badge/all_contributors-387-orange.svg?style=flat-square)](#contributors-)
1010
<!-- ALL-CONTRIBUTORS-BADGE:END -->
1111

1212
<br/>
@@ -570,6 +570,12 @@ This project is licensed under the terms of the MIT license.
570570
<td align="center" valign="top" width="16.66%"><a href="https://github.com/sanurah"><img src="https://avatars.githubusercontent.com/u/16178588?v=4?s=100" width="100px;" alt="Sanura Hettiarachchi"/><br /><sub><b>Sanura Hettiarachchi</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=sanurah" title="Code">💻</a></td>
571571
<td align="center" valign="top" width="16.66%"><a href="https://github.com/2897robo"><img src="https://avatars.githubusercontent.com/u/31699375?v=4?s=100" width="100px;" alt="Kim Gi Uk"/><br /><sub><b>Kim Gi Uk</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=2897robo" title="Code">💻</a></td>
572572
<td align="center" valign="top" width="16.66%"><a href="https://github.com/Suchismita-Deb"><img src="https://avatars.githubusercontent.com/u/68535074?v=4?s=100" width="100px;" alt="Suchismita Deb"/><br /><sub><b>Suchismita Deb</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Suchismita-Deb" title="Code">💻</a></td>
573+
<td align="center" valign="top" width="16.66%"><a href="https://github.com/ssrijan-007-sys"><img src="https://avatars.githubusercontent.com/u/137605821?v=4?s=100" width="100px;" alt="ssrijan-007-sys"/><br /><sub><b>ssrijan-007-sys</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ssrijan-007-sys" title="Code">💻</a></td>
574+
</tr>
575+
<tr>
576+
<td align="center" valign="top" width="16.66%"><a href="https://github.com/e5LA"><img src="https://avatars.githubusercontent.com/u/208197507?v=4?s=100" width="100px;" alt="e5LA"/><br /><sub><b>e5LA</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=e5LA" title="Code">💻</a></td>
577+
<td align="center" valign="top" width="16.66%"><a href="http://maziyar-gerami.github.io/portfolio/"><img src="https://avatars.githubusercontent.com/u/122622721?v=4?s=100" width="100px;" alt="Maziyar Gerami"/><br /><sub><b>Maziyar Gerami</b></sub></a><br /><a href="#translation-maziyar-gerami" title="Translation">🌍</a></td>
578+
<td align="center" valign="top" width="16.66%"><a href="https://github.com/yybmion"><img src="https://avatars.githubusercontent.com/u/113106136?v=4?s=100" width="100px;" alt="yoobin_mion"/><br /><sub><b>yoobin_mion</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=yybmion" title="Code">💻</a></td>
573579
</tr>
574580
</tbody>
575581
</table>

actor-model/README.md

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
title: "Actor Model Pattern in Java: Building Concurrent Systems with Elegance"
3+
shortTitle: Actor Model
4+
description: "Explore the Actor Model pattern in Java with real-world examples and practical implementation. Learn how to build scalable, message-driven systems using actors, messages, and asynchronous communication."
5+
category: Concurrency
6+
language: en
7+
tag:
8+
- Concurrency
9+
- Messaging
10+
- Isolation
11+
- Asynchronous
12+
- Distributed Systems
13+
- Actor Model
14+
---
15+
16+
## Also Known As
17+
18+
- Message-passing concurrency
19+
- Actor-based concurrency
20+
21+
---
22+
23+
## Intent of Actor Model Pattern
24+
25+
The Actor Model pattern enables the construction of highly concurrent, distributed, and fault-tolerant systems by using isolated components (actors) that interact exclusively through asynchronous message passing.
26+
27+
---
28+
29+
## Detailed Explanation of Actor Model Pattern with Real-World Examples
30+
31+
### 📦 Real-world Example
32+
33+
Imagine a customer service system:
34+
- Each **customer support agent** is an **actor**.
35+
- Customers **send questions (messages)** to agents.
36+
- Each agent handles one request at a time and can **respond asynchronously** without interfering with other agents.
37+
38+
---
39+
40+
### 🧠 In Plain Words
41+
42+
> "Actors are like independent workers that never share memory and only communicate through messages."
43+
44+
---
45+
46+
### 📖 Wikipedia Says
47+
48+
> [Actor model](https://en.wikipedia.org/wiki/Actor_model) is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent computation.
49+
50+
---
51+
52+
### 🧹 Architecture Diagram
53+
54+
![UML Class Diagram](./etc/Actor_Model_UML_Class_Diagram.png)
55+
56+
---
57+
58+
## Programmatic Example of Actor Model Pattern in Java
59+
60+
### Actor.java
61+
62+
```java
63+
public abstract class Actor implements Runnable {
64+
65+
@Setter @Getter private String actorId;
66+
private final BlockingQueue<Message> mailbox = new LinkedBlockingQueue<>();
67+
private volatile boolean active = true;
68+
69+
70+
public void send(Message message) {
71+
mailbox.add(message);
72+
}
73+
74+
public void stop() {
75+
active = false;
76+
}
77+
78+
@Override
79+
public void run() {
80+
81+
}
82+
83+
protected abstract void onReceive(Message message);
84+
}
85+
86+
```
87+
88+
### Message.java
89+
90+
```java
91+
92+
@AllArgsConstructor
93+
@Getter
94+
@Setter
95+
public class Message {
96+
private final String content;
97+
private final String senderId;
98+
}
99+
```
100+
101+
### ActorSystem.java
102+
103+
```java
104+
public class ActorSystem {
105+
public void startActor(Actor actor) {
106+
String actorId = "actor-" + idCounter.incrementAndGet(); // Generate a new and unique ID
107+
actor.setActorId(actorId); // assign the actor it's ID
108+
actorRegister.put(actorId, actor); // Register and save the actor with it's ID
109+
executor.submit(actor); // Run the actor in a thread
110+
}
111+
public Actor getActorById(String actorId) {
112+
return actorRegister.get(actorId); // Find by Id
113+
}
114+
115+
public void shutdown() {
116+
executor.shutdownNow(); // Stop all threads
117+
}
118+
}
119+
```
120+
121+
### App.java
122+
123+
```java
124+
public class App {
125+
public static void main(String[] args) {
126+
ActorSystem system = new ActorSystem();
127+
Actor srijan = new ExampleActor(system);
128+
Actor ansh = new ExampleActor2(system);
129+
130+
system.startActor(srijan);
131+
system.startActor(ansh);
132+
ansh.send(new Message("Hello ansh", srijan.getActorId()));
133+
srijan.send(new Message("Hello srijan!", ansh.getActorId()));
134+
135+
Thread.sleep(1000); // Give time for messages to process
136+
137+
srijan.stop(); // Stop the actor gracefully
138+
ansh.stop();
139+
system.shutdown(); // Stop the actor system
140+
}
141+
}
142+
```
143+
144+
---
145+
146+
## When to Use the Actor Model Pattern in Java
147+
148+
- When building **concurrent or distributed systems**
149+
- When you want **no shared mutable state**
150+
- When you need **asynchronous, message-driven communication**
151+
- When components should be **isolated and loosely coupled**
152+
153+
---
154+
155+
## Actor Model Pattern Java Tutorials
156+
157+
- [Baeldung – Akka with Java](https://www.baeldung.com/java-akka)
158+
- [Vaughn Vernon – Reactive Messaging Patterns](https://vaughnvernon.co/?p=1143)
159+
160+
---
161+
162+
## Real-World Applications of Actor Model Pattern in Java
163+
164+
- [Akka Framework](https://akka.io/)
165+
- [Erlang and Elixir concurrency](https://www.erlang.org/)
166+
- [Microsoft Orleans](https://learn.microsoft.com/en-us/dotnet/orleans/)
167+
- JVM-based game engines and simulators
168+
169+
---
170+
171+
## Benefits and Trade-offs of Actor Model Pattern
172+
173+
### ✅ Benefits
174+
- High concurrency support
175+
- Easy scaling across threads or machines
176+
- Fault isolation and recovery
177+
- Message ordering within actors
178+
179+
### ⚠️ Trade-offs
180+
- Harder to debug due to asynchronous behavior
181+
- Slight performance overhead due to message queues
182+
- More complex to design than simple method calls
183+
184+
---
185+
186+
## Related Java Design Patterns
187+
188+
- [Command Pattern](../command)
189+
- [Mediator Pattern](../mediator)
190+
- [Event-Driven Architecture](../event-driven-architecture)
191+
- [Observer Pattern](../observer)
192+
193+
---
194+
195+
## References and Credits
196+
197+
- *Programming Erlang*, Joe Armstrong
198+
- *Reactive Design Patterns*, Roland Kuhn
199+
- *The Actor Model in 10 Minutes*, [InfoQ Article](https://www.infoq.com/articles/actor-model/)
200+
- [Akka Documentation](https://doc.akka.io/docs/akka/current/index.html)
201+
Loading

actor-model/etc/actor-model.urm.puml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@startuml actor-model
2+
3+
title Actor Model - UML Class Diagram
4+
5+
class ActorSystem {
6+
+actorOf(actor: Actor): Actor
7+
+shutdown(): void
8+
}
9+
10+
class Actor {
11+
-mailbox: BlockingQueue<Message>
12+
-active: boolean
13+
+send(message: Message): void
14+
+stop(): void
15+
+run(): void
16+
#onReceive(message: Message): void
17+
}
18+
19+
class ExampleActor {
20+
+onReceive(message: Message): void
21+
}
22+
23+
class Message {
24+
-content: String
25+
-sender: Actor
26+
+getContent(): String
27+
+getSender(): Actor
28+
}
29+
30+
ActorSystem --> Actor : creates
31+
Actor <|-- ExampleActor : extends
32+
Actor --> Message : processes
33+
ExampleActor --> Message : uses
34+
35+
@enduml

0 commit comments

Comments
 (0)