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

الخيوط الافتراضية (Virtual Threads)

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

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

نظرة عامة

الموضوعات

  • Project Loom
  • الخيوط الافتراضية
  • اعتبارات التثبيت (pinning)
  • عمليات الإدخال والإخراج الحاجبة (blocking I/O)

تعلّم أولًا

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

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

  1. Explain how virtual threads differ from platform threads and why they suit blocking I/O.
  2. Enable virtual threads in Spring Boot and measure the effect.
  3. Identify pinning and other cases where virtual threads do not help (CPU-bound work, limited downstream resources).
  4. Explain why resource limits (connection pools, semaphores) still matter.

الدروس

  1. 01مشكلة خيط لكل طلب
  2. 02شرح الخيوط الافتراضية
  3. 03الخيوط الافتراضية في Spring Boot
  4. 04التثبيت (Pinning) ومزالق أخرى
  5. 05التزامن المهيكل (حالة المعاينة)
  6. 06عرض عملي: الخيوط الأساسية مقابل الافتراضية تحت الحِمل

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

Lab

MID-007-LAB1 · Load comparison

Outcomes: 1–4 · Time: ~2.5 h Run Shop's slow-supplier endpoint under identical load with platform threads and with virtual threads. Record throughput, latency percentiles, and database pool saturation. Add a limit (semaphore or pool size) and re-run. Acceptance: results explained, including the new bottleneck.

Exercises (assessment: scenario quiz on when virtual threads help)

IDOutcomeTask
MID-007-EX011Explain in five sentences how a virtual thread differs from a platform thread and what happens when it blocks.
MID-007-EX021Run 10,000 tasks that sleep 1 s on a fixed pool of 200 and on a virtual-thread-per-task executor; compare times.
MID-007-EX032Enable virtual threads in Shop and prove via a log of Thread.currentThread() that requests run on virtual threads.
MID-007-EX043For four workloads (JDBC-heavy, blocking HTTP, image processing, file hashing), say whether virtual threads help.
MID-007-EX054Add a Semaphore(20) to protect a downstream client and show requests queueing safely.
MID-007-EX063Describe how you would detect pinning in a running service.
أظهر الحلول
  • EX01 Virtual threads are JVM-managed and cheap; they mount on carrier threads and unmount when blocked on supported I/O, freeing the carrier.
  • EX02 The pool takes about 10,000/200 x 1 s = 50 s; virtual threads take about 1-2 s.
  • EX03 Log Thread.currentThread().isVirtual() in a controller; it prints true when enabled.
  • EX04 JDBC-heavy: helps up to the pool limit. Blocking HTTP: helps. Image processing and hashing: CPU-bound, no gain.
  • EX05 Acquire before the call, release in finally; excess threads wait cheaply.
  • EX06 Use JFR's virtual-thread pinned event or the tracing flag (verify for the JDK in use), then check for blocking inside synchronized or native code.
  • Outcome 2's measurement is also covered by the lab.

الاختبار

Outcomes: O1 how they work · O2 Spring Boot and measuring · O3 pinning and limits · O4 resource limits.

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

  1. 1A virtual thread blocked on supported I/O:O1
  2. 2Virtual threads are best for:O1
  3. 3You should create virtual threads:O1
  4. 4The Spring Boot property to enable them is:O2
  5. 5After enabling them you should:O2
  6. 6Pinning means:O3
  7. 7A CPU-bound endpoint on virtual threads will:O3
  8. 8The core idea of structured concurrency is:O1
  9. 9With 10,000 virtual threads and a 10-connection pool, the bottleneck is:O4
  10. 10To protect a limited downstream service you should:O4

الجزء العملي

Six exercises MID-007-EX01 to EX06 (including workload scenarios on when virtual threads help) plus the lab. Pass: at least five exercises correct and the lab explaining the new bottleneck.

أخطاء شائعة

Pooling virtual threads; expecting CPU-bound speed-ups; removing all concurrency limits.

التقييم

Scenario quiz: when virtual threads help.

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

Modern Java service design.

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