Skip to content

Commit 46fe841

Browse files
docs: Update README.md (#2652)
Added explanation, programming example.
1 parent be914e0 commit 46fe841

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

half-sync-half-async/README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,133 @@ Use Half-Sync/Half-Async pattern when
2323
* the higher level tasks in the system can be simplified significantly if I/O is performed synchronously.
2424
* one or more tasks in a system must run in a single thread of control, while other tasks may benefit from multi-threading.
2525

26+
## Explanation
27+
The Half-Sync/Half-Async pattern is a concurrency design pattern used in software engineering to manage concurrent operations and interactions in a system. It's particularly useful in applications where both asynchronous and synchronous processing are required to achieve optimal performance and responsiveness.
28+
29+
The pattern is structured into two components: the synchronous part (Half-Sync) and the asynchronous part (Half-Async).
30+
31+
Half-Sync (Synchronous Part):
32+
In this part, there is a synchronous layer that handles high-level control flow and coordination of the application. It typically consists of a set of threads that handle communication, synchronization, and coordination. These threads are responsible for managing shared resources and orchestrating the flow of work in a synchronous manner.
33+
34+
Half-Async (Asynchronous Part):
35+
The asynchronous part involves a set of asynchronous workers or threads that perform the actual processing and execution of tasks. These threads are responsible for carrying out time-consuming or potentially blocking operations asynchronously. They handle I/O operations, long-running computations, or any task that can be parallelized effectively.
36+
37+
How it Works:
38+
39+
Synchronous Part:
40+
41+
The synchronous part handles incoming requests, organizes the tasks to be performed, and dispatches them to the asynchronous workers.
42+
It's responsible for high-level orchestration, resource management, and synchronization of the overall system.
43+
Asynchronous Part:
44+
45+
Asynchronous workers execute the tasks independently and concurrently without blocking the main application.
46+
They handle time-consuming or I/O-bound tasks efficiently, ensuring the system remains responsive.
47+
Advantages:
48+
49+
Responsiveness: Asynchronous processing ensures that the system remains responsive by allowing non-blocking operations.
50+
Parallel Execution: It enables concurrent execution of tasks, improving performance by utilizing multiple threads or processes.
51+
Scalability: The asynchronous part can be scaled easily to handle a larger number of requests efficiently.
52+
Disadvantages:
53+
54+
Complexity: Implementing and managing the two parts can be complex, requiring careful coordination and synchronization mechanisms.
55+
Potential Deadlocks: The interplay between the synchronous and asynchronous parts can introduce potential deadlocks or race conditions if not handled correctly.
56+
The Half-Sync/Half-Async pattern strikes a balance between responsiveness and efficiency by utilizing both synchronous and asynchronous processing to optimize the performance of a system while ensuring responsiveness to user requests.
57+
58+
## Programmatic Example
59+
```java
60+
{
61+
import java.util.LinkedList;
62+
import java.util.Queue;
63+
64+
class TaskQueue {
65+
private Queue<String> queue = new LinkedList<>();
66+
67+
synchronized void enqueue(String task) {
68+
queue.add(task);
69+
notify(); // Notify waiting threads that a task is available
70+
}
71+
72+
synchronized String dequeue() throws InterruptedException {
73+
while (queue.isEmpty()) {
74+
wait(); // Wait until a task is enqueued
75+
}
76+
return queue.poll();
77+
}
78+
}
79+
80+
class SynchronousPart extends Thread {
81+
private TaskQueue taskQueue;
82+
83+
public SynchronousPart(TaskQueue taskQueue) {
84+
this.taskQueue = taskQueue;
85+
}
86+
87+
@Override
88+
public void run() {
89+
while (true) {
90+
try {
91+
String task = taskQueue.dequeue();
92+
System.out.println("Synchronous part processing task: " + task);
93+
// Simulate some synchronous processing
94+
Thread.sleep(1000);
95+
} catch (InterruptedException e) {
96+
Thread.currentThread().interrupt();
97+
}
98+
}
99+
}
100+
}
101+
102+
class AsynchronousPart extends Thread {
103+
@Override
104+
public void run() {
105+
while (true) {
106+
// Simulate some asynchronous processing
107+
System.out.println("Asynchronous part processing tasks...");
108+
try {
109+
Thread.sleep(500);
110+
} catch (InterruptedException e) {
111+
Thread.currentThread().interrupt();
112+
}
113+
}
114+
}
115+
}
116+
117+
public class HalfSyncHalfAsyncExample {
118+
public static void main(String[] args) {
119+
TaskQueue taskQueue = new TaskQueue();
120+
121+
SynchronousPart synchronousThread = new SynchronousPart(taskQueue);
122+
AsynchronousPart asynchronousThread = new AsynchronousPart();
123+
124+
synchronousThread.start();
125+
asynchronousThread.start();
126+
127+
// Enqueue tasks
128+
taskQueue.enqueue("Task 1");
129+
taskQueue.enqueue("Task 2");
130+
taskQueue.enqueue("Task 3");
131+
132+
// Allow the threads to run for a while
133+
try {
134+
Thread.sleep(5000);
135+
} catch (InterruptedException e) {
136+
Thread.currentThread().interrupt();
137+
}
138+
139+
// Stop the threads (in a real application, you would have a proper way to signal the threads to stop)
140+
synchronousThread.interrupt();
141+
asynchronousThread.interrupt();
142+
}
143+
}
144+
```
145+
### In this Java example:
146+
147+
* TaskQueue class manages the task queue and provides methods for enqueueing and dequeuing tasks in a thread-safe manner.
148+
* SynchronousPart and AsynchronousPart classes represent the synchronous and asynchronous parts of the application, respectively.
149+
* The synchronous part processes tasks in a synchronous manner, and the asynchronous part processes tasks in an asynchronous manner, simulating some processing time.
150+
* The main program creates instances of TaskQueue, SynchronousPart, and AsynchronousPart, enqueues tasks, and starts the threads to demonstrate the Half-Sync/Half-Async pattern.
151+
152+
26153
## Real world examples
27154

28155
* [BSD Unix networking subsystem](https://www.dre.vanderbilt.edu/~schmidt/PDF/PLoP-95.pdf)

0 commit comments

Comments
 (0)