Concurrency and virtual threads: Java 25 practice questions
Concurrency and virtual threads is 10% of the free Java 25 mock (5 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.
J.VI · Java 25 practice question
An application passes a security token from a controller through six intermediate service layers to a repository method. What drawback of standard method-parameter passing does ScopedValue<T> help eliminate?
Check answer and explanation
Correct answer: Every intermediate method in the call chain must declare the parameter and thereby gains access to the data.
Explanation
Passing data through method parameters forces every intermediate method in the sequence to declare that parameter and exposes the data to each of them, which ScopedValue avoids.
Common trap
Claiming a four-frame limit on reference parameters is tempting as a fabricated JVM constraint, but Java imposes no such limit on method parameters. Claiming the JVM deep-copies objects on each call is tempting to developers thinking of pass-by-value semantics in other languages, whereas Java passes object references without deep copying.
Source: Java SE 25 API: ScopedValue
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
A backend service can handle at most 20 simultaneous calls to a downstream payment gateway, and the application uses virtual threads. What is the recommended way to enforce that concurrency limit in Java 25?
Check answer and explanation
Correct answer: Use a concurrency-limiting construct such as a Semaphore rather than pooling virtual threads.
Explanation
Using a concurrency-limiting construct such as a Semaphore is the right approach because virtual threads are not meant to be pooled to cap resource access.
Common trap
Creating a fixed-size pool of 20 virtual threads is tempting because developers routinely used thread pools for throttling with platform threads, but JEP 444 explicitly advises against pooling virtual threads for this purpose. Capping carrier threads to 20 is tempting as a global limit, but it restricts unrelated virtual threads across the JVM instead of guarding the specific resource.
Source: JEP 444: Virtual Threads — Description
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
How does ExecutorService extend the basic Executor interface in the Java SE 25 standard library?
Check answer and explanation
Correct answer: It adds methods to manage termination and produce Future objects for tracking asynchronous tasks.
Explanation
ExecutorService builds on Executor by providing termination-management methods and methods that return a Future to track the progress of asynchronous tasks.
Common trap
Assuming sequential single-thread execution is tempting because Executors.newSingleThreadExecutor returns an ExecutorService, but the interface itself supports multi-threaded pools like ForkJoinPool and ThreadPoolExecutor. Restricting tasks to virtual threads is tempting in a Java 25 context, yet ExecutorService supports both platform-thread and virtual-thread implementations.
Source: Java SE 25 API: ExecutorService
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
How does the Java 25 runtime schedule virtual threads onto physical processor cores?
Check answer and explanation
Correct answer: The JDK's scheduler assigns virtual threads to platform threads, which the OS then schedules onto processor cores.
Explanation
Virtual threads use M:N scheduling: the JDK scheduler mounts virtual threads onto platform threads, and the OS schedules those platform threads on CPU cores as usual.
Common trap
Having the OS kernel schedule virtual threads directly is tempting because that is how platform threads work, but virtual threads are managed by the JDK's own scheduler. Bypassing the OS scheduler to assign virtual threads directly to CPU cores is tempting as a misunderstanding of user-mode scheduling, yet the JDK still relies on OS-scheduled platform threads.
Source: JEP 444: Virtual Threads — Description
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
A developer needs a concurrency type that can both be completed explicitly with a result and chain dependent actions that trigger upon completion. Which pair of interfaces implemented by CompletableFuture<T> provides these capabilities?
Check answer and explanation
Correct answer: Future<T> and CompletionStage<T>
Explanation
CompletableFuture<T> implements Future<T> and CompletionStage<T>, combining explicit completion of a future value with composable dependent functions and actions.
Common trap
Choosing Future<T> and AutoCloseable is tempting because ExecutorService implements AutoCloseable, but CompletableFuture does not implement AutoCloseable. Choosing Runnable and CompletionStage<T> is tempting because FutureTask implements Runnable, whereas CompletableFuture implements Future<T> rather than Runnable.
Source: Java SE 25 API: CompletableFuture
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
What is printed when this Java 25 program runs?
class Demo {
static final ScopedValue<String> CONTEXT = ScopedValue.newInstance();
static void readKey() {
System.out.println(CONTEXT.get());
}
static void userCode() {
readKey();
}
public static void main(String[] args) {
ScopedValue.where(CONTEXT, "tenant-7").run(() -> userCode());
}
}
Check answer and explanation
Correct answer: tenant-7
Explanation
Because readKey is called within the dynamic extent of the run call, CONTEXT.get() retrieves the bound value tenant-7 and prints it.
Common trap
Returning null is tempting if a developer assumes ScopedValue defaults to null like an uninitialized ThreadLocal, but CONTEXT is bound for the entire call stack inside run. Throwing NoSuchElementException is tempting if someone thinks the binding only applies to the immediate lambda and not nested calls like readKey, yet any method called from run sees the bound value.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
What is printed when this Java 25 program runs?
import java.util.concurrent.CompletableFuture;
class Demo {
public static void main(String[] args) {
CompletableFuture<String> cf = new CompletableFuture<>();
boolean first = cf.complete("ready");
boolean second = cf.complete("override");
boolean canceled = cf.cancel(true);
System.out.println(first + ":" + second + ":" + canceled + ":" + cf.join());
}
}
Check answer and explanation
Correct answer: true:false:false:ready
Explanation
Only the first completion attempt on a CompletableFuture succeeds; subsequent calls to complete or cancel return false and leave the initial value ready intact.
Common trap
Returning true:true:false:override is tempting if a developer thinks complete overwrites an existing result like AtomicReference.set, but a CompletableFuture can be completed only once. Returning true:false:true:ready is tempting if someone assumes cancel can always abort a future even after completion, yet cancel also fails once completed.
Source: Java SE 25 API: CompletableFuture
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
A service creates an executor with Executors.newFixedThreadPool(4, factory). All four worker threads are currently busy executing long-running tasks when a fifth task is submitted. What happens to the fifth task?
Check answer and explanation
Correct answer: The task waits in a shared unbounded queue until one of the four threads becomes available.
Explanation
A fixed thread pool operates with at most nThreads active threads off a shared unbounded queue, so additional tasks wait in that queue until a worker thread is free.
Common trap
Spawning a temporary fifth thread is tempting because cached thread pools grow on demand, whereas newFixedThreadPool caps active threads at nThreads. Throwing RejectedExecutionException is tempting if a developer assumes the pool uses a bounded queue, but newFixedThreadPool uses an unbounded queue.
Source: Java SE 25 API: Executors.newFixedThreadPool
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
A developer starts a new Thread from the main thread to run a background computation. According to the Java SE 25 Thread specification, how does the newly started thread execute relative to the thread that started it?
Check answer and explanation
Correct answer: The new thread executes concurrently with the thread that started it.
Explanation
Once started, the newly created thread is scheduled to run concurrently alongside the thread that invoked start.
Common trap
Blocking the starting thread automatically is tempting for developers confusing start() with a synchronous call or join(), but start returns immediately while the new thread runs concurrently. Waiting until the starting thread exits its current method is tempting as a perceived safety guarantee, yet the JVM does not delay thread execution until the caller returns.
Source: Java SE 25 API: Thread
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
In a thread-per-request web framework that uses ScopedValue, application code handling a request spawns its own virtual threads to perform parallel subtasks. How are those spawned virtual threads related to the request-handling thread?
Check answer and explanation
Correct answer: They are child threads of the request-handling thread.
Explanation
When user code running inside a request creates its own virtual threads to run subtasks, those virtual threads are child threads of the request-handling thread.
Common trap
Treating them as detached threads with no relationship is tempting if a developer thinks virtual threads are independent pooled workers, whereas JEP 506 specifically describes them as child threads of the request-handling thread. Replacing the request-handling thread is tempting if someone confuses spawning subtasks with thread migration, yet the original request thread remains the parent.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
A developer tries to define a custom subclass of ScopedValue to add logging hooks. What happens when this Java 25 code is compiled?
class CustomScope<T> extends ScopedValue<T> {
}
class Demo {
public static void main(String[] args) {
System.out.println("ok");
}
}
Check answer and explanation
Correct answer: Compilation fails because ScopedValue is declared final and cannot be subclassed.
Explanation
ScopedValue<T> is declared as a public final class in java.lang, so attempting to extend it causes a compile-time error.
Common trap
Expecting compilation to succeed is tempting because ThreadLocal can be subclassed (for example to override initialValue), but ScopedValue is final. Blaming a missing get() override is tempting if someone thinks ScopedValue is abstract, yet the class is final and cannot be extended regardless of methods overridden.
Source: Java SE 25 API: ScopedValue
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
A web framework binds a request context with ScopedValue and invokes user code inside a run call. How does data flow between the framework's entry method and deeper methods invoked during that run call?
Check answer and explanation
Correct answer: Data is shared in one direction from the caller of run to downstream methods for the duration of that run call.
Explanation
ScopedValue's run method shares data one way from the binding caller to methods invoked inside run, and the binding lasts only for the lifetime of that run call.
Common trap
Allowing user code to overwrite the value for the caller is tempting because ThreadLocal permits two-way mutation via set(), whereas ScopedValue enforces one-way sharing. Keeping the binding alive until explicitly cleared is also a ThreadLocal habit that ScopedValue eliminates by scoping the binding strictly to run.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
While an executor created by Executors.newFixedThreadPool(4, factory) is still running (before shutdown), one of its worker threads terminates due to an uncaught failure during task execution. What happens to the pool's capacity for subsequent tasks?
Check answer and explanation
Correct answer: A new thread is created to replace the terminated one if needed for subsequent tasks.
Explanation
When a worker thread in a fixed thread pool dies from a failure prior to shutdown, the pool replaces it with a new thread if needed to run later tasks.
Common trap
Permanently shrinking the pool to three threads is tempting if a developer assumes fixed thread pools pre-create threads once and never recreate them, but the pool automatically replaces failed workers. Shutting down the entire executor is tempting if someone expects fail-fast pool termination on any task exception, whereas newFixedThreadPool continues serving subsequent tasks.
Source: Java SE 25 API: Executors.newFixedThreadPool
Checked against the cited text on 2026-11-25.
J.VI · Java 25 practice question
Why did submitting tasks to a fixed-size thread pool become such a widespread pattern for limiting concurrent access to a scarce resource in Java applications?
Check answer and explanation
Correct answer: Platform threads are expensive to create and maintain, which made thread pools standard practice.
Explanation
Platform threads carry a high resource cost, so thread pools became standard across Java codebases and developers reused them to bound concurrent resource access.
Common trap
Claiming Java lacked semaphore primitives is tempting if someone assumes concurrency limiters are new, but Semaphore has existed in java.util.concurrent for decades. Blaming operating system socket limits on thread pools is tempting when thinking about network saturation, but the OS does not require a thread pool to accept connections.
Source: JEP 444: Virtual Threads — Description
Checked against the cited text on 2026-11-25.