كل التخصصاتالتقنية · مقابلة عمل

التوازي في Java

تختبر أسئلة التزامن قدرتك على التفكير في مشكلات يصعب رؤيتها. وشرحها بالإنجليزية يعني إيجاد كلمات بسيطة لأفكار صعبة.

كلمات → عبارات → جمل → تحدث

جرّب SpeakRole ↗
مقابلة عملالتوازي في Java
V

What is a race condition, and how do you prevent it in Java?

0:18
سؤال المقابل

التوازي في Java

التزامن هو الموضع الذي يميّز فيه المحاورون بين من قرأ عن الخيوط ومن تتبّع أخطاءها بنفسه. توقّع مزيجًا من التعريفات والسيناريوهات الصغيرة: ما الذي يسوء، ولماذا يسوء، وكيف ستصلحه.

الإجابة الشفهية الجيدة تسمّي المشكلة أولًا، ثم تعرض مثالًا قصيرًا بالكلمات، ثم تقترح حلًّا أو حلّين مع كلفة كل منهما. اذكر الكلفة دائمًا، فلكل حل لمشكلات التزامن كلفة.

  • ✓كلمات → عبارات → جمل → تحدث
  • ✓معنى الإجابة
  • ✓اللغة

أكثر الأسئلة تكرارًا ونماذج الإجابات

What is a race condition, and how do you prevent it in Java?

ما حالة التسابق (race condition) وكيف تمنعها في Java؟

ما الذي يريد المحاور سماعه. مثال محدد، مثل عدّاد بلا مزامنة، وأكثر من طريقة واحدة للإصلاح.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

A race condition happens when the result depends on the timing of threads. A classic example is a counter: two threads read the same value, both add one, and both write it back, so one update is lost. To prevent it, I can make the operation atomic with AtomicInteger, protect the critical section with synchronized or a ReentrantLock, or avoid shared mutable state completely by using immutable objects or keeping data inside one thread. I prefer the last option when possible, because it removes the problem instead of managing it.

What is the difference between volatile and synchronized?

ما الفرق بين volatile وsynchronized؟

ما الذي يريد المحاور سماعه. الرؤية (visibility) مقابل الذرّية (atomicity) والاستبعاد المتبادل (mutual exclusion).

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

Volatile guarantees visibility. When one thread writes a volatile variable, other threads see the new value. But volatile does not make a compound action atomic. For example, count plus plus on a volatile integer is still a race. Synchronized gives mutual exclusion, so only one thread enters the block at a time, and it also guarantees visibility when the lock is released and acquired. I use volatile for simple flags, like a stop signal, and synchronized or atomic classes when I update state in several steps.

What is a deadlock, and how do you avoid it?

ما الجمود المتبادل (deadlock) وكيف تتجنبه؟

ما الذي يريد المحاور سماعه. صورة واضحة لخيطين ينتظر كل منهما الآخر، وطرق عملية للوقاية.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

A deadlock happens when two or more threads wait for each other's locks forever. Thread A holds lock one and wants lock two, while thread B holds lock two and wants lock one. To avoid it, I always acquire locks in the same global order. I keep critical sections short and I do not call unknown code while holding a lock. Another option is tryLock with a timeout, so a thread can give up and retry. If I suspect a deadlock in production, I take a thread dump, and the JVM usually shows the cycle clearly.

Why use a thread pool instead of creating threads manually?

لماذا نستخدم مجمّع خيوط (thread pool) بدل إنشاء الخيوط يدويًا؟

ما الذي يريد المحاور سماعه. كلفة إنشاء الخيوط، وتحديد الموارد، وخطر حقيقي واحد في الإعداد.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

Creating a thread is expensive, and unlimited threads can exhaust memory and slow everything down. A thread pool reuses a fixed number of threads and puts extra tasks in a queue, which gives us control over the load. I use ExecutorService, and I prefer to configure ThreadPoolExecutor directly, because some factory methods use an unbounded queue, and that can hide a memory problem. For blocking input and output in Java 21, I would also consider virtual threads, because they are cheap and make the code simpler.

How is ConcurrentHashMap different from a synchronized map?

بماذا يختلف ConcurrentHashMap عن الخريطة المتزامنة (synchronized map)؟

ما الذي يريد المحاور سماعه. كيف يعمل القفل، ولماذا تهم العمليات المركّبة.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

A map wrapped with Collections.synchronizedMap uses one lock for the whole map, so threads wait for each other even when they touch different keys. ConcurrentHashMap allows many threads to read and write at the same time, with very fine-grained locking and compare-and-swap operations inside. It also has atomic methods like putIfAbsent, compute and merge. These are important, because a check followed by a put is not atomic on any map. One difference to remember is that ConcurrentHashMap does not allow null keys or null values.

كيف تنطق المصطلحات الأساسية

thread
ثْرِد (الثاء كما في كلمة «ثلاثة»)
mutex
مِيُوتِكْس
deadlock
دِدْلُوك
atomic
أَتُومِك
volatile
ڤُولَتِل (بالأمريكية) أو ڤُولَتَايل (بالبريطانية)
asynchronous
إِيسِنْكْرِنِس

عبارات مفيدة للإجابة

  • The problem is that two threads can…المشكلة أن خيطين قد…
  • To make this thread-safe, I would…لجعل هذا آمنًا للخيوط (thread-safe)، سأقوم بـ…
  • The cost of that approach is…كلفة هذا الأسلوب هي…
  • In a real project, I would measure first.في مشروع حقيقي سأقيس الأداء أولًا.
  • Let me walk you through a scenario.دعني أستعرض معك سيناريو.
  • I would take a thread dump to see where the threads are blocked.سألتقط تفريغًا للخيوط (thread dump) لأرى أين حُجبت الخيوط.

أخطاء شائعة

  • الخلط بين التزامن (concurrency) والتوازي (parallelism). التزامن يعني التعامل مع مهام كثيرة في آن واحد، والتوازي يعني تنفيذها في اللحظة نفسها على عدة أنوية. تكفي جملة واحدة.
  • قول «استخدم synchronized في كل مكان». هذا يدل على أنك لا تقدّر الكلفة. اذكر التنافس على القفل (contention) وحجم الجزء المقفول.
  • وصف المشكلة نظريًا فقط. مثال من جملتين، مثل عدّاد مشترك، أسهل في الفهم وفي التذكّر.
  • نسيان ذكر كيف ستجد الخطأ. اذكر تفريغات الخيوط (thread dumps) والسجلات التي تتضمن أسماء الخيوط واختبارات الإجهاد.

الأسئلة الشائعة

هل أحتاج إلى معرفة نموذج ذاكرة Java بالتفصيل؟

ينبغي أن تفهم فكرة happens-before ولماذا تهم الرؤية (visibility)، وأن تشرحها ببساطة. القواعد الدقيقة لإعادة الترتيب لا تلزم عادةً إلا في الوظائف العليا.

هل الخيوط الافتراضية موضوع شائع في المقابلات الآن؟

يزداد شيوعها إذا كان الفريق يستخدم Java 21 أو أحدث. كن قادرًا على قول الغرض منها (عدد كبير من المهام التي تحجب الخيط) وما لا تحله (الأعمال الثقيلة على المعالج ومشكلات الحالة المشتركة).

كيف أشرح خطأ تزامن أصلحته؟

استخدم قصة قصيرة: ما العَرَض الذي ظهر، وكيف وجدت السبب، وماذا غيّرت، وكيف أثبتّ أن المشكلة حُلّت. يعجب المحاورين غالبًا مسار تتبّع الخطأ أكثر من الكود النهائي.

قراءة الإجابة ليست مثل قولها. تدرّب على كل سؤال بصوت عالٍ، ثم قارن معنى إجابتك ولغتها بالنموذج.

جرّب SpeakRole ↗

موضوعات أخرى في هذا المجال