You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Prioritize requests sent to services so that requests with a higher priority are received and
13
-
processed more quickly than those of a lower priority. This pattern is useful in applications that
14
-
offer different service level guarantees to individual clients.
19
+
The Priority Queue design pattern provides a way to manage a collection of elements where each element has a priority, and elements are accessed and removed based on their priority rather than their insertion order.
15
20
16
21
## Explanation
17
22
18
-
Applications may delegate specific tasks to other services; for example, to perform background
19
-
processing or to integrate with other applications or services. In the cloud, a message queue is
20
-
typically used to delegate tasks to background processing. In many cases the order in which requests
21
-
are received by a service is not important. However, in some cases it may be necessary to prioritize
22
-
specific requests. These requests should be processed earlier than others of a lower priority that
23
-
may have been sent previously by the application.
24
-
25
23
Real world example
26
24
27
-
> Imagine a video processing service with free and premium customers. The requests coming from the
28
-
> paying premium customers should be prioritized over the others.
25
+
> Imagine an emergency room in a hospital. Patients arrive with varying degrees of urgency: some have minor injuries, while others have life-threatening conditions. The hospital uses a priority queue system to manage these patients. Instead of treating patients on a first-come, first-served basis, the medical staff assigns a priority level to each patient based on the severity of their condition. Patients with more critical conditions (higher priority) are treated before those with less severe issues, ensuring that urgent cases receive immediate attention. This system efficiently manages resources and prioritizes care, similar to how a priority queue handles elements based on their priority.
29
26
30
27
In plain words
31
28
32
-
> Priority Queue enables processing of high priority messages first, regardless of queue size or
33
-
> message age.
29
+
> Priority Queue enables processing of high priority messages first, regardless of queue size or message age.
34
30
35
31
Wikipedia says
36
32
37
-
> In computer science, a priority queue is an abstract data type similar to regular queue or stack
38
-
> data structure in which each element additionally has a "priority" associated with it. In a
39
-
> priority queue, an element with high priority is served before an element with low priority.
33
+
> In computer science, a priority queue is an abstract data type similar to regular queue or stack data structure in which each element additionally has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority.
40
34
41
35
**Programmatic Example**
42
36
43
-
Looking at the video processing example from above, let's first see the `Message` structure.
37
+
Looking at a video processing example, let's first see the `Message` structure.
44
38
45
39
```java
46
40
publicclassMessageimplementsComparable<Message> {
47
41
48
-
privatefinalString message;
49
-
privatefinalint priority; // define message priority in queue
42
+
privatefinalString message;
43
+
privatefinalint priority; // define message priority in queue
50
44
51
-
publicMessage(Stringmessage, intpriority) {
52
-
this.message = message;
53
-
this.priority = priority;
54
-
}
45
+
publicMessage(Stringmessage, intpriority) {
46
+
this.message = message;
47
+
this.priority = priority;
48
+
}
55
49
56
-
@Override
57
-
publicintcompareTo(Messageo) {
58
-
return priority - o.priority;
59
-
}
60
-
...
50
+
@Override
51
+
publicintcompareTo(Messageo) {
52
+
return priority - o.priority;
53
+
}
54
+
//...
61
55
}
62
56
```
63
57
64
-
Here's `PriorityMessageQueue` that handles storing the messages and serving them in priority
65
-
order.
58
+
Here's `PriorityMessageQueue` that handles storing the messages and serving them in priority order.
0 commit comments