انتقل إلى المحتوى
أمين أبوزيد
درس مخطط لهMID-005-L01

لماذا لا يكفي synchronizedMap

  • الدرس 1 من 5
  • Mid-level
في هذا الدرس 4 موضوعات

هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.

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

في هذا الدرس

see why per-call locking does not make compound actions safe.

الموضوعات المخطط لها

  • Collections.synchronizedMap locks each call, so single calls are safe.
  • A sequence such as get then put is two calls; another thread can run between them.
  • Iterating needs manual locking on the map, or you risk ConcurrentModificationException.
  • One lock for the whole map limits throughput.
  • Use collections designed for concurrency.
تفاصيل المخطط المبدئي

عرض عملي

java
Integer n = map.get(sku);             // another thread may update here
map.put(sku, n == null ? 1 : n + 1);  // lost update

اختبر فهمك

Why is get then put unsafe on a synchronized map?

أظهر الإجابة

The pair is not atomic; another thread can interleave.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

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

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

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