التزامن في Java
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- الخيوط (Threads)
- Runnable
- Callable
- ExecutorService
- مجمّعات الخيوط (Thread Pools)
تعلّم أولًا
4 ساعة تدريب (تقدير)
مخرجات التعلم
- Create and coordinate threads and explain thread states.
- Submit
RunnableandCallabletasks to anExecutorServiceand collectFutureresults. - Size a thread pool for CPU-bound vs I/O-bound work and explain queueing behavior.
- Shut executors down correctly and handle task failures.
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| MID-003-EX01 | 1 | Print a thread's state at NEW, after start, while in sleep, while blocked on a monitor, and after finish. |
| MID-003-EX02 | 2 | Submit three Callable<Integer> tasks, one throwing; collect results and show how the exception appears. |
| MID-003-EX03 | 3 | Size a pool for 8 cores: (a) image resizing, (b) supplier HTTP calls waiting 400 ms with 100 ms compute. |
| MID-003-EX04 | 3 | Predict how many threads run for core 4, max 16, LinkedBlockingQueue unbounded and 100 tasks; then for ArrayBlockingQueue(10). |
| MID-003-EX05 | 4 | Write a shutdown helper using shutdown, awaitTermination, shutdownNow. |
| MID-003-EX06 | 4 | Make a pool reject work visibly when full and count rejections. |
أظهر الحلول
- EX01
getState(): NEW, RUNNABLE, TIMED_WAITING, BLOCKED, TERMINATED. - EX02
Future.get()throwsExecutionException;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
ThreadPoolExecutorwith a bounded queue and a customRejectedExecutionHandlerthat increments anAtomicIntegerand 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).