J.VI · Java 25 practice question
Consider the following Java code snippet utilizing virtual threads in Java 25. The `Worker` class uses a `ReentrantLock` to protect a shared counter, while an alternative approach might use the `synchronized` keyword on the `increment` method. ```java import java.util.concurrent.locks.ReentrantLock; class SharedResource { private int counter = 0; private final ReentrantLock lock = new ReentrantLock(); public void increment() { lock.lock(); try { // Simulate some work that might be blocking/I/O bound Thread.sleep(10); counter++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { lock.unlock(); } } public int getCounter() { return counter; } } public class VirtualThreadTest { public static void main(String[] args) throws Exception { SharedResource resource = new SharedResource(); // Create and run multiple virtual threads concurrently Runnable task = () -> { for (int i = 0; i < 100; i++) { resource.increment(); } }; Thread.ofVirtual().start(task); Thread.ofVirtual().start(task); Thread.ofVirtual().start(task); // Wait for threads to finish (simplified wait for demonstration) Thread.sleep(500); System.out.println("Final Counter: " + resource.getCounter()); } } ``` Assuming the `Thread.sleep(10)` inside the critical section simulates an I/O-bound operation that benefits significantly from virtual threads' non-blocking nature, what is the *primary* performance difference expected between using `ReentrantLock` (as shown) and replacing the lock mechanism with a simple `synchronized` block on the `increment` method when running with many virtual threads?
Check answer and explanation
Correct answer: Both mechanisms will exhibit similar low overhead, as the core synchronization primitive (locking) is independent of whether the thread is platform or virtual.
Explanation
Both `ReentrantLock` and `synchronized` use underlying OS/JVM synchronization mechanisms (like monitors or explicit lock structures). When running on virtual threads (Project Loom), the overhead of *managing* the lock itself is largely abstracted away, and the benefits of virtual threads are seen in how they handle blocking (like `Thread.sleep`). For simple mutual exclusion, the performance difference between well-implemented `ReentrantLock` and `synchronized` in this scenario is generally minimal, as both enforce the same atomic access control.
Common trap
Options A and D suggest one mechanism is fundamentally better for virtual threads, which is overly simplistic. While `ReentrantLock` offers more features (like timed waits or fairness), when compared directly against `synchronized` for simple mutual exclusion in a virtual thread context, the performance impact of the lock structure itself is dominated by the fact that the thread suspension is lightweight.
Source: Java Platform Module System documentation or Project Loom JEPs (https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/locks/Lock.html)
Checked against the cited text on 2026-11-27.