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

التزامن في Java

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

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

نظرة عامة

الموضوعات

  • الخيوط (Threads)
  • Runnable
  • Callable
  • ExecutorService
  • مجمّعات الخيوط (Thread Pools)

تعلّم أولًا

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

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

  1. Create and coordinate threads and explain thread states.
  2. Submit Runnable and Callable tasks to an ExecutorService and collect Future results.
  3. Size a thread pool for CPU-bound vs I/O-bound work and explain queueing behavior.
  4. Shut executors down correctly and handle task failures.

الدروس

  1. 01لماذا التزامن: الإنتاجية وزمن الاستجابة
  2. 02الخيوط ودورة حياتها
  3. 03المنفّذات والنتائج المستقبلية (Executors وFutures)
  4. 04مجمّعات الخيوط: الحجم والطابور والرفض
  5. 05الإيقاف ومعالجة الأخطاء
  6. 06عرض عملي: تنفيذ استيراد أسعار الخلاصة بالتوازي
  7. 07عرض عملي: مجمّع خيوط يُسقط العمل بصمت

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

Lab

MID-003-LAB1 · Parallel import

Outcomes: 1–4 · Time: ~4 h Import product prices from several simulated suppliers (varying latency, one failing, one timing out) concurrently with a bounded pool, per-task timeouts, and a failure report. Shut the pool down correctly. Compare wall-clock time with the sequential version. Acceptance: wall-clock improvement measured; no lost tasks (imported + failed + timed out equals submitted).

Exercises (assessment: pool-sizing scenarios plus the lab)

IDOutcomeTask
MID-003-EX011Print a thread's state at NEW, after start, while in sleep, while blocked on a monitor, and after finish.
MID-003-EX022Submit three Callable<Integer> tasks, one throwing; collect results and show how the exception appears.
MID-003-EX033Size a pool for 8 cores: (a) image resizing, (b) supplier HTTP calls waiting 400 ms with 100 ms compute.
MID-003-EX043Predict how many threads run for core 4, max 16, LinkedBlockingQueue unbounded and 100 tasks; then for ArrayBlockingQueue(10).
MID-003-EX054Write a shutdown helper using shutdown, awaitTermination, shutdownNow.
MID-003-EX064Make a pool reject work visibly when full and count rejections.
أظهر الحلول
  • EX01 getState(): NEW, RUNNABLE, TIMED_WAITING, BLOCKED, TERMINATED.
  • EX02 Future.get() throws ExecutionException; getCause() is the task's exception; the other results still arrive.
  • EX03 (a) about 8 threads. (b) cores x (1 + 400/100) = 8 x 5 = 40 threads as a starting point; then measure.
  • EX04 Unbounded queue: 4 threads, 96 queued. Bounded queue of 10: 4 core threads + 10 queued, then up to 12 extra threads (max 16) for the next 12 tasks, remaining tasks rejected: 100 - 26 = 74 rejected under the default abort policy if all submitted at once.
  • EX05 pool.shutdown(); if (!pool.awaitTermination(t, SECONDS)) pool.shutdownNow(); with interrupt handling around the wait.
  • EX06 ThreadPoolExecutor with a bounded queue and a custom RejectedExecutionHandler that increments an AtomicInteger and throws or runs on the caller.
  • Outcome 1's coordination is also in the lab; the L02 join/interrupt points are checked in Q02 and Q03.

الاختبار

Outcomes: O1 threads and states · O2 executors and futures · O3 pool sizing · O4 shutdown and failures.

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

  1. 1To run code on a new thread you call:O1
  2. 2A thread waiting to enter a synchronized block is in state:O1
  3. 3join() on a thread:O1
  4. 4Callable differs from Runnable by:O2
  5. 5A task's exception reaches the caller of Future.get() as:O2
  6. 6A good starting size for a CPU-bound pool is about:O3
  7. 7With an unbounded queue and core size 4, max 16, extra threads beyond 4 are:O3
  8. 8Which factory has an unbounded thread count?O3
  9. 9Correct shutdown order is:O4
  10. 10Which policy silently loses tasks?O4

الجزء العملي

Six exercises MID-003-EX01 to EX06 (including two pool-sizing scenarios) plus the lab. Pass: at least five exercises correct and the lab showing a measured speed-up with no lost tasks.

أخطاء شائعة

Unbounded queues; newCachedThreadPool under load; never calling shutdown.

التقييم

Pool-sizing scenarios plus lab.

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

Tomcat and connection-pool sizing (MID-017).

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