انتقل إلى المحتوى
أمين أبوزيد
MID-005

المجموعات المتزامنة

  • 5 درس
  • 2.5 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • ConcurrentHashMap
  • الطوابير الحاجبة (blocking queues)
  • هياكل النسخ عند الكتابة (copy-on-write)

تعلّم أولًا

2.5 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Use ConcurrentHashMap atomic operations (compute, merge, putIfAbsent) correctly.
  2. Build producer-consumer pipelines with BlockingQueue.
  3. Explain when copy-on-write collections fit and when they hurt.

الدروس

  1. 01لماذا لا يكفي synchronizedMap
  2. 02ConcurrentHashMap
  3. 03الطوابير الحاجبة والمنتج والمستهلك
  4. 04هياكل النسخ عند الكتابة
  5. 05عرض عملي: خط معالجة طلبات محدود بنمط المنتج والمستهلك

المختبر والتمارين

Lab

MID-005-LAB1 · Order event buffer

Outcomes: 1–3 (mainly 2) · Time: ~2.5 h Buffer Shop order events in a bounded queue consumed by worker threads. Count events per product with a ConcurrentHashMap. Shut down cleanly with poison pills. Acceptance: producer slows when the queue is full; no events lost (produced equals consumed).

Exercises (assessment: 4 exercises)

IDOutcomeTask
MID-005-EX011Count words from 4 threads using HashMap and get/put; show lost updates; fix with merge.
MID-005-EX021Rewrite a containsKey/put cache fill with computeIfAbsent; explain what to avoid inside the function.
MID-005-EX032Build a producer-consumer with ArrayBlockingQueue(10); show blocking put and a clean poison-pill stop.
MID-005-EX043For three cases (listener list, 500k-entry list written often, small read-mostly config), say whether copy-on-write fits and why.
أظهر الحلول
  • EX01 The HashMap total is below the expected count. map.merge(word, 1, Integer::sum) on a ConcurrentHashMap gives the exact total.
  • EX02 cache.computeIfAbsent(k, this::load). Keep the function short, do not block on slow I/O if avoidable, and never modify the same map inside it.
  • EX03 The producer blocks when 10 items wait. Stop by putting one poison object per consumer; each consumer exits on receiving it.
  • EX04 Listener list: yes (read-mostly, small). 500k list written often: no (copy per write). Small read-mostly config: yes.
  • The lab's backpressure criterion also covers outcome 2 in practice.

الاختبار

Outcomes: O1 ConcurrentHashMap · O2 blocking queues · O3 copy-on-write.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Why is get then put on a synchronized map unsafe?O1
  2. 2Which call atomically counts occurrences?O1
  3. 3ConcurrentHashMap does not allow:O1
  4. 4Inside a compute function you should:O1
  5. 5BlockingQueue.put on a full queue:O2
  6. 6Which gives backpressure?O2
  7. 7A common way to stop consumers cleanly is:O2
  8. 8An unbounded queue under a fast producer risks:O2
  9. 9Copy-on-write best fits:O3
  10. 10A CopyOnWriteArrayList iterator:O3

الجزء العملي

Four exercises MID-005-EX01 to EX04 plus the lab. Pass: at least three exercises correct and the lab showing backpressure with no lost events.

أخطاء شائعة

get then put on a concurrent map; unbounded queues.

التقييم

4 exercises.

الصلة بالمسار الوظيفي

Backpressure in messaging (SRB-006).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.