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

أمان الخيوط (Thread Safety)

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

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

نظرة عامة

الموضوعات

  • حالات التسابق (Race Conditions)
  • المزامنة (synchronization)
  • الأقفال (locks)
  • volatile
  • العمليات الذرية (atomics)

تعلّم أولًا

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

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

  1. Identify race conditions and check-then-act bugs in code review.
  2. Explain visibility and the Java Memory Model's happens-before at a practical level.
  3. Choose between synchronized, ReentrantLock, volatile, and atomics.
  4. Prefer immutability and confinement over locking, and avoid deadlocks with lock ordering.

الدروس

  1. 01حالات التسابق
  2. 02الرؤية وعلاقة happens-before
  3. 03synchronized والأقفال الداخلية
  4. 04الأقفال الصريحة
  5. 05volatile والعمليات الذرية
  6. 06عدم القابلية للتغيير والحصر
  7. 07الجمود المتبادل (Deadlocks)
  8. 08عرض عملي: إعادة إنتاج حالة تسابق باختبار إجهاد
  9. 09عرض عملي: تشخيص جمود متبادل من thread dump

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

Lab

MID-004-LAB1 · Stock counter

Outcomes: 1–4 · Time: ~4 h Take an in-memory stock counter with a check-then-act race. Write a stress test that fails. Fix it three ways: (1) a lock (synchronized or ReentrantLock), (2) AtomicInteger with compare-and-set, (3) an immutable snapshot swapped through AtomicReference. Compare correctness, throughput, and code clarity. Acceptance: a stress test fails before and passes after each fix.

Exercises (assessment: five snippets with concurrency bugs)

IDOutcomeTask
MID-004-EX011Snippet: lazy singleton if (inst == null) inst = new X();. Identify the race and fix it.
MID-004-EX021Snippet: if (!map.containsKey(k)) map.put(k, v) on a synchronizedMap. Explain the bug and fix it.
MID-004-EX032Snippet: a plain boolean stop flag read in a loop. Explain the visibility problem and fix it.
MID-004-EX044Snippet: a @Service with a List<Order> field. Explain the risk and two redesigns.
MID-004-EX054Snippet: two methods locking a then b, and b then a. Show the deadlock and the fix.
أظهر الحلول
  • EX01 Two threads can both see null. Fix with a holder class, an enum, synchronized, or volatile plus double-checked locking.
  • EX02 The check and put are separate; use ConcurrentHashMap.putIfAbsent or computeIfAbsent.
  • EX03 Without a happens-before edge the loop may never see the write; make the flag volatile (or use interruption).
  • EX04 One shared instance across requests; make it stateless, pass data as parameters, or use a thread-safe structure or per-request object.
  • EX05 Thread 1 holds a waiting for b; thread 2 the reverse. Always lock in one order (for example by id).
  • Outcome 3 (choice of tool) is covered by the lab's three fixes and Q05-Q08 (including volatile with ++, which needs an atomic).

الاختبار

Outcomes: O1 races · O2 visibility and happens-before · O3 choosing tools · O4 immutability and deadlocks.

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

  1. 1if (stock > 0) stock--; on shared state is an example of:O1
  2. 2count++ is unsafe because it is:O1
  3. 3Which creates a happens-before edge?O2
  4. 4A plain flag read in a loop may:O2
  5. 5volatile alone is enough for:O3
  6. 6For a single lock-free counter, prefer:O3
  7. 7ReentrantLock is chosen over synchronized mainly to get:O3
  8. 8Where does lock.unlock() belong?O3
  9. 9A safe lock object is:O4
  10. 10Deadlock from opposite lock order is prevented by:O4

الجزء العملي

Five exercises MID-004-EX01 to EX05 (the five concurrency-bug snippets) plus the lab. Pass: at least four snippets correct and a stress test that fails before and passes after each of the three fixes.

أخطاء شائعة

volatile for compound actions; locking on mutable or public objects; state in Spring singletons.

التقييم

Find concurrency bugs in five snippets.

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

Essential for any shared state; distributed analogues in MID-032.

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