لماذا لا يكفي synchronizedMap
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
see why per-call locking does not make compound actions safe.
الموضوعات المخطط لها
Collections.synchronizedMaplocks each call, so single calls are safe.- A sequence such as
getthenputis 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.
تفاصيل المخطط المبدئي
عرض عملي
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.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.