Skip to content

Commit b4a5840

Browse files
authored
Merge pull request #212 from javaevolved/copilot/wait-notify-to-blocking-queue
Add "Wait and notify queues to BlockingQueue" concurrency pattern
2 parents 5174b7e + a4c23fa commit b4a5840

19 files changed

Lines changed: 336 additions & 2 deletions

content/concurrency/lock-free-lazy-init.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ support:
5050
state: "preview"
5151
description: "Preview in JDK 25 (JEP 502, StableValue). Requires --enable-preview."
5252
prev: "concurrency/concurrent-http-virtual"
53-
next: "concurrency/thread-stop-to-cooperative-cancellation"
53+
next: "concurrency/wait-notify-to-blocking-queue"
5454
related:
5555
- "concurrency/virtual-threads"
5656
- "concurrency/structured-concurrency"

content/concurrency/thread-stop-to-cooperative-cancellation.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ whyModernWins:
3838
support:
3939
state: "available"
4040
description: "Future and ExecutorService have been available since JDK 5 (Sept 2004)."
41-
prev: "concurrency/lock-free-lazy-init"
41+
prev: "concurrency/wait-notify-to-blocking-queue"
4242
next: "io/http-client"
4343
related:
4444
- "concurrency/executor-try-with-resources"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
id: "6d265fc2-1b92-417b-a1dd-9150d4985a1b"
3+
slug: "wait-notify-to-blocking-queue"
4+
title: "Wait and notify queues to BlockingQueue"
5+
category: "concurrency"
6+
difficulty: "intermediate"
7+
jdkVersion: "5"
8+
oldLabel: "Manual wait and notify"
9+
modernLabel: "BlockingQueue"
10+
oldApproach: "Manual wait and notify"
11+
modernApproach: "BlockingQueue"
12+
oldCode: |-
13+
synchronized (queue) {
14+
while (queue.isEmpty()) {
15+
queue.wait();
16+
}
17+
Job job = queue.removeFirst();
18+
process(job);
19+
}
20+
modernCode: |-
21+
BlockingQueue<Job> queue = new LinkedBlockingQueue<>();
22+
23+
Job job = queue.take();
24+
process(job);
25+
summary: "Replace hand-written producer-consumer coordination with BlockingQueue."
26+
explanation: "Correct wait/notify code requires a shared monitor, condition loops,\
27+
\ matching notifications, and careful interruption handling. BlockingQueue encapsulates\
28+
\ those rules and provides blocking, timed, and non-blocking operations for producer-consumer\
29+
\ workflows. Calls such as put() and take() remain interruptible."
30+
whyModernWins:
31+
- icon: "🛡"
32+
title: "Safer coordination"
33+
desc: "The queue owns the locking and condition signaling."
34+
- icon: "👁"
35+
title: "Clear intent"
36+
desc: "put() and take() directly express producer-consumer behavior."
37+
- icon: "🎛"
38+
title: "Built-in policies"
39+
desc: "Choose bounded capacity, fairness, timeouts, or non-blocking operations."
40+
support:
41+
state: "available"
42+
description: "Widely available since JDK 5 (2004)"
43+
prev: "concurrency/lock-free-lazy-init"
44+
next: "concurrency/thread-stop-to-cooperative-cancellation"
45+
related:
46+
- "concurrency/executor-try-with-resources"
47+
- "concurrency/structured-concurrency"
48+
- "concurrency/virtual-threads"
49+
tags:
50+
- concurrency
51+
docs:
52+
- title: "BlockingQueue"
53+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/BlockingQueue.html"
54+
- title: "LinkedBlockingQueue"
55+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/LinkedBlockingQueue.html"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
//JAVA 25+
3+
import java.util.concurrent.*;
4+
5+
/// Proof: wait-notify-to-blocking-queue
6+
/// Source: content/concurrency/wait-notify-to-blocking-queue.yaml
7+
void main() throws Exception {
8+
record Job(String name) {}
9+
10+
BlockingQueue<Job> queue = new LinkedBlockingQueue<>();
11+
queue.put(new Job("build"));
12+
13+
Job job = queue.take();
14+
process(job);
15+
}
16+
17+
void process(Object job) {
18+
System.out.println("processing " + job);
19+
}

social/queue.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ language/raw-collections-to-generics
131131
concurrency/thread-stop-to-cooperative-cancellation
132132
io/explicit-charset-file-io
133133
tooling/class-newinstance-to-constructor
134+
concurrency/wait-notify-to-blocking-queue

social/tweets.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,4 +1327,14 @@ tooling/class-newinstance-to-constructor: |-
13271327
13281328
🔗 https://javaevolved.github.io/tooling/class-newinstance-to-constructor.html
13291329
1330+
#Java #JavaEvolved
1331+
concurrency/wait-notify-to-blocking-queue: |-
1332+
☕ Wait and notify queues to BlockingQueue
1333+
1334+
Replace hand-written producer-consumer coordination with BlockingQueue.
1335+
1336+
Manual wait and notify → BlockingQueue (JDK 5+)
1337+
1338+
🔗 https://javaevolved.github.io/concurrency/wait-notify-to-blocking-queue.html
1339+
13301340
#Java #JavaEvolved
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
title: "تحويل طوابير wait و notify إلى BlockingQueue"
2+
oldApproach: "wait و notify يدوياً"
3+
modernApproach: "BlockingQueue"
4+
summary: "استبدل تنسيق المنتج-المستهلك المكتوب يدوياً باستخدام BlockingQueue."
5+
explanation: "يتطلب كود wait/notify الصحيح مراقباً مشتركاً وحلقات شرطية وإشعارات متطابقة ومعالجة دقيقة للمقاطعة. تُغلّف BlockingQueue هذه القواعد وتوفر عمليات حاجزة ومحددة زمنياً وغير حاجزة لسير عمل المنتج-المستهلك. تبقى استدعاءات مثل put() و take() قابلة للمقاطعة."
6+
whyModernWins:
7+
- icon: "🛡"
8+
title: "تنسيق أكثر أماناً"
9+
desc: "يتولى الطابور القفل والإشارة الشرطية."
10+
- icon: "👁"
11+
title: "نية واضحة"
12+
desc: "تعبّر put() و take() مباشرة عن سلوك المنتج-المستهلك."
13+
- icon: "🎛"
14+
title: "سياسات مدمجة"
15+
desc: "اختر سعة محدودة أو العدالة أو المهلات أو العمليات غير الحاجزة."
16+
support:
17+
description: "متاح على نطاق واسع منذ JDK 5 (2004)"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
title: wait/notify কিউ থেকে BlockingQueue
2+
oldApproach: ম্যানুয়াল wait এবং notify
3+
modernApproach: BlockingQueue
4+
summary: হাতে লেখা প্রোডিউসার-কনজিউমার সমন্বয়কে BlockingQueue দিয়ে প্রতিস্থাপন করুন।
5+
explanation: সঠিক wait/notify কোডের জন্য একটি শেয়ার্ড মনিটর, কন্ডিশন লুপ, মিলিত নোটিফিকেশন
6+
এবং সতর্ক ইন্টারাপশন হ্যান্ডলিং প্রয়োজন। BlockingQueue এই নিয়মগুলোকে এনক্যাপসুলেট করে
7+
এবং প্রোডিউসার-কনজিউমার ওয়ার্কফ্লোর জন্য ব্লকিং, টাইমড এবং নন-ব্লকিং অপারেশন প্রদান করে।
8+
put() এবং take()-এর মতো কলগুলো এখনও ইন্টারাপ্ট করা যায়।
9+
whyModernWins:
10+
- icon: "🛡"
11+
title: নিরাপদ সমন্বয়
12+
desc: কিউ নিজেই লকিং এবং কন্ডিশন সিগন্যালিং পরিচালনা করে।
13+
- icon: "👁"
14+
title: স্পষ্ট উদ্দেশ্য
15+
desc: put() এবং take() সরাসরি প্রোডিউসার-কনজিউমার আচরণ প্রকাশ করে।
16+
- icon: "🎛"
17+
title: বিল্ট-ইন নীতি
18+
desc: সীমিত ক্যাপাসিটি, ফেয়ারনেস, টাইমআউট বা নন-ব্লকিং অপারেশন বেছে নিন।
19+
support:
20+
description: JDK 5 (2004) থেকে ব্যাপকভাবে উপলব্ধ
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
title: Wait/notify-Warteschlangen zu BlockingQueue
2+
oldApproach: Manuelles wait und notify
3+
modernApproach: BlockingQueue
4+
summary: Ersetze handgeschriebene Producer-Consumer-Koordination durch BlockingQueue.
5+
explanation: "Korrekter wait/notify-Code erfordert einen gemeinsamen Monitor, Bedingungsschleifen,\
6+
\ passende Benachrichtigungen und sorgfältige Behandlung von Interrupts. BlockingQueue\
7+
\ kapselt diese Regeln und bietet blockierende, zeitgesteuerte und nicht-blockierende\
8+
\ Operationen für Producer-Consumer-Workflows. Aufrufe wie put() und take() bleiben\
9+
\ unterbrechbar."
10+
whyModernWins:
11+
- icon: "🛡"
12+
title: Sicherere Koordination
13+
desc: Die Queue übernimmt Sperren und Bedingungssignalisierung.
14+
- icon: "👁"
15+
title: Klare Absicht
16+
desc: put() und take() drücken Producer-Consumer-Verhalten direkt aus.
17+
- icon: "🎛"
18+
title: Eingebaute Richtlinien
19+
desc: Wähle begrenzte Kapazität, Fairness, Timeouts oder nicht-blockierende Operationen.
20+
support:
21+
description: Weitgehend verfügbar seit JDK 5 (2004)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
title: Colas con wait y notify a BlockingQueue
2+
oldApproach: wait y notify manuales
3+
modernApproach: BlockingQueue
4+
summary: Reemplaza la coordinación productor-consumidor escrita a mano con BlockingQueue.
5+
explanation: El código correcto de wait/notify requiere un monitor compartido, bucles
6+
de condición, notificaciones coincidentes y un manejo cuidadoso de la interrupción.
7+
BlockingQueue encapsula esas reglas y ofrece operaciones bloqueantes, con tiempo
8+
límite y no bloqueantes para flujos productor-consumidor. Llamadas como put() y
9+
take() siguen siendo interrumpibles.
10+
whyModernWins:
11+
- icon: 🛡
12+
title: Coordinación más segura
13+
desc: La cola se encarga del bloqueo y la señalización de condiciones.
14+
- icon: 👁
15+
title: Intención clara
16+
desc: put() y take() expresan directamente el comportamiento productor-consumidor.
17+
- icon: 🎛
18+
title: Políticas integradas
19+
desc: Elige capacidad limitada, equidad, tiempos de espera u operaciones no bloqueantes.
20+
support:
21+
description: Ampliamente disponible desde JDK 5 (2004)

0 commit comments

Comments
 (0)