โ† 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 `Phaser` method allows a thread to arrive at a phase and deregister itself from future phases?

    Answer: arriveAndDeregister()

    `arriveAndDeregister()` records arrival and reduces the registered party count, so the thread no longer participates in subsequent phases.

  2. What distinguishes `ScheduledThreadPoolExecutor` from a regular `ThreadPoolExecutor`?

    Answer: It can schedule commands to run after a delay or periodically

    `ScheduledThreadPoolExecutor` adds the ability to schedule tasks with fixed-delay, fixed-rate, or one-shot delay execution.

  3. When using `synchronized` on an instance method, which object is used as the monitor lock?

    Answer: The instance (`this`) on which the method is called

    An instance `synchronized` method acquires the intrinsic lock on `this`, the object the method is invoked on.

  4. Which scenario can cause a `ConcurrentModificationException` in a non-concurrent collection?

    Answer: Modifying a collection while iterating over it with a for-each loop

    Structurally modifying a collection outside of the iterator's own `remove()` during iteration triggers a `ConcurrentModificationException`.

  5. What is the key difference between `ReentrantLock` and `ReentrantReadWriteLock`?

    Answer: ReadWriteLock allows multiple concurrent readers but only one writer at a time

    `ReentrantReadWriteLock` separates read and write locks, allowing concurrent reads but exclusive writes for better throughput in read-heavy workloads.

  6. Which `CompletableFuture` method combines two futures and applies a function to both results when both complete?

    Answer: thenCombine()

    `thenCombine()` waits for both `CompletableFuture`s to complete and applies a `BiFunction` to their results, returning a new `CompletableFuture`.

  7. What is the effect of calling `LockSupport.park()` on a thread that already has a pending unpark permit?

    Answer: It returns immediately without blocking

    If a permit is available from a prior `unpark()`, `park()` consumes it and returns immediately without blocking.