Java Concurrency Flashcards
7 cards from real OCP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Java Concurrency flashcards as text
Which `java.util.concurrent.atomic` class provides an atomic reference to an object that can be updated using compare-and-swap?
Answer: AtomicReference
`AtomicReference` wraps an object reference and provides atomic CAS operations via `compareAndSet()`.
What is a happens-before relationship in the Java Memory Model?
Answer: A guarantee that results of action A are visible to action B if A happens-before B
The happens-before relationship guarantees that memory writes by one action are visible to another action that follows it in the happens-before order.
What exception is thrown when a thread waiting on a `Future.get()` is interrupted?
Answer: InterruptedException
`Future.get()` throws `InterruptedException` if the current thread is interrupted while waiting for the computation.
Which method of `Semaphore` acquires a permit, blocking until one is available or the thread is interrupted?
Answer: acquire()
`acquire()` blocks indefinitely until a permit is available or the calling thread is interrupted.
In a `ForkJoinPool`, what does a worker thread do when its own deque is empty?
Answer: Steals tasks from the tail of another worker's deque
Work-stealing allows idle workers to steal tasks from the tail (opposite end) of busy workers' deques to maximize CPU utilization.
Which class is the preferred replacement for `Vector` in concurrent Java programs?
Answer: CopyOnWriteArrayList
`CopyOnWriteArrayList` is thread-safe for iteration without locking by creating a new copy of the array on each mutative operation.
What is the result of calling `Thread.interrupted()` on a thread whose interrupt flag is set?
Answer: Returns true and clears the interrupt flag
`Thread.interrupted()` is a static method that returns the current thread's interrupt status and then clears it.