ConcurrentHashMap
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
use atomic map operations correctly.
الموضوعات المخطط لها
- Fine-grained internal locking and lock-free reads allow high concurrency.
putIfAbsent,computeIfAbsent,compute,mergeare atomic per key.- Keep the function passed to
compute/mergeshort, side-effect free, and non-blocking; do not update the same map inside it. - No
nullkeys or values. size()and iteration are weakly consistent; they give a snapshot-like view, not a frozen one. Do not use them for exact decisions.
تفاصيل المخطط المبدئي
عرض عملي
Map<String, LongAdder> views = new ConcurrentHashMap<>();
views.computeIfAbsent(sku, k -> new LongAdder()).increment();
Map<String, Integer> stock = new ConcurrentHashMap<>();
stock.merge(sku, 1, Integer::sum);اختبر فهمك
Which call replaces get-then-put counting?
أظهر الإجابة
merge(key, 1, Integer::sum).
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.