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

volatile والعمليات الذرية

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

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

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

في هذا الدرس

choose volatile or atomic classes correctly.

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

  • volatile gives visibility and ordering for single reads and writes; not atomic compound actions.
  • Good for flags and safe publication of immutable objects.
  • AtomicInteger, AtomicLong, AtomicReference use compare-and-set for lock-free updates.
  • LongAdder scales better than AtomicLong under heavy contention.
  • Atomics protect one variable; invariants across several variables still need a lock.
تفاصيل المخطط المبدئي

عرض عملي

java
private final AtomicInteger stock = new AtomicInteger(100);
boolean reserve(int q) {
    int cur;
    do { cur = stock.get(); if (cur < q) return false; }
    while (!stock.compareAndSet(cur, cur - q));
    return true;
}

اختبر فهمك

Does volatile int count; count++ fix the race?

أظهر الإجابة

No; ++ is still three steps.

التطبيق

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

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

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

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