عرض عملي: إعادة إنتاج حالة تسابق باختبار إجهاد
في هذا الدرس 3 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
prove a race exists before fixing it; prepares MID-004-LAB1.
تفاصيل المخطط المبدئي
خطوة بخطوة
- Write
StockCounterwithreserve(int)using check-then-act. - Stress test: 100 threads each reserve 1 unit from 1,000, started together with a
CountDownLatch; assert final stock is 900. - Run repeatedly; the test fails intermittently (stock above 900, or oversold).
- Planned mistake: add
volatileto the field and re-run; it still fails. Explain: visibility is not atomicity. - Fix with
synchronized; the test passes across many runs. - Note that a passing run is evidence only when the test failed before the fix.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.