โ† All OCP Flashcard Decks

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
  1. Which method of the `CountDownLatch` class blocks the calling thread until the count reaches zero?

    Answer: await()

    `await()` blocks the current thread until the latch's count reaches zero, while `countDown()` decrements the count.

  2. What happens when a thread calls `CyclicBarrier.await()` and it is the last thread to arrive at the barrier?

    Answer: It triggers the optional barrier action and releases all waiting threads

    The last thread to call `await()` optionally runs the barrier action, then all waiting threads are released.

  3. Which `ExecutorService` method submits a `Callable` and returns a `Future` representing the pending result?

    Answer: submit(Callable)

    `submit(Callable)` accepts a `Callable` and returns a `Future` that can be used to retrieve the result.

  4. What is the purpose of the `volatile` keyword in Java concurrency?

    Answer: Ensures visibility of variable changes across threads without atomicity guarantees

    `volatile` guarantees that reads/writes go directly to main memory, ensuring visibility, but does not provide atomicity for compound operations.

  5. Which class in `java.util.concurrent` provides a resizable-array implementation of a blocking queue?

    Answer: LinkedBlockingQueue

    `LinkedBlockingQueue` is backed by linked nodes and can optionally be bounded, while `ArrayBlockingQueue` uses a fixed-size array.

  6. What does `Future.cancel(true)` do to a running task?

    Answer: Attempts to interrupt the thread executing the task

    Passing `true` to `cancel()` sends an interrupt signal to the thread running the task, but the task must check for interruption to stop.

  7. Which `ReentrantLock` feature allows a thread to acquire the lock only if it is not held by another thread at the time of invocation?

    Answer: tryLock()

    `tryLock()` returns `true` immediately if the lock is available or `false` if another thread holds it, without blocking.