These activities accompany chapters C19–C24. Perform them on copies of the sources with JDK 25. C19–C21 and C24 tests use stable APIs; C23 requires the incubator module declared in the chapter. The criteria help distinguish an observed result from an API guarantee.
C19-A01 – An unrequested fallback
Task. In the Optional program, predict how many times defaultName() is called when lookup finds a book and the caller first uses orElse(defaultName()), then orElseGet(OptionalDemo::defaultName). Run and compare. Repeat with an empty lookup.
Criterion and solution. In the present case, orElse still evaluates its argument; orElseGet does not call the supplier. In the empty case, both forms compute the fallback once per call. The difference concerns when the argument is evaluated, rather than the final value if the supplier always returns the same string.
C19-P01 – Which absence are we describing?
Task. Add a book with no author and compare find(code).map(OptionalDemo::author) with find(code).flatMap(OptionalDemo::author). Write the type of both expressions and explain what an empty result after flatMap means.
Criterion and solution. map produces Optional<Optional<String>>; flatMap produces Optional<String>. The latter may be empty because the book is missing or because it has no author. If the application must explain which case occurred, flattening alone is insufficient.
C20-A01 – run does not start the thread
Task. Before running StartAndJoin, predict the lines with the thread name and the states before start, after direct run and after join. Explain why join makes the final observed state reliable.
Criterion and solution. Directly called work prints main and does not change NEW. Work after start prints worker; after join, execution has finished and the state is TERMINATED. The explanation must refer to the contract of start and join, rather than machine speed.
C20-P01 – Counting without relying on luck
Task. In the protected counter, identify the monitor used by the two threads and explain why the result is 20000. In a copy, remove synchronized only from increment, run several times and record results, without assuming every execution shows the defect.
Criterion and solution. The correct version protects increments with the same monitor and main waits for both workers. The modified version contains a data race and does not guarantee 20000, though it may print it by chance. The test must not fail because one defective execution happens to appear correct.
C20-D01 – The end of two consumers
Task. In the producer–consumer program, imagine starting two consumers on the same queue. How many end sentinels are needed for both to leave the loop? Explain why you cannot predict which consumer will take each value.
Criterion and solution. With that protocol, one sentinel is needed per consumer. The queue assigns each element to just one take, but consumer selection depends on scheduling. A correct test checks that values are consumed once and both workers finish, rather than the order of their names in printed output.
C20-V01 – When to choose a virtual thread
Task. A service receives an average of 2,000 requests per second. Each lasts 50 milliseconds, mainly because it waits for a remote archive. Estimate how many requests are concurrently in progress and explain which platform-thread limit motivates virtual threads. The archive, however, accepts at most 20 simultaneous accesses: propose an explicit limit. Finally, repeat the choice for work computing without waits on all available cores.
Criterion and solution. On average, about 100 requests are in progress (2,000 × 0.050). One platform thread per request retains an operating-system thread even while waiting; a virtual thread can suspend and release the carrier, while keeping code linear. More virtual threads do not increase archive capacity: a Semaphore with 20 permits can limit access. For work continuously using the CPU, the limit is in the cores rather than waiting; creating many virtual threads does not accelerate the individual computation. The answer must distinguish concurrency, throughput and latency.
C21-A01 – Bootstrap's null
Task. Run ObserveClassLoader. Identify which class returns null from getClassLoader() and explain why this does not indicate a missing class. Avoid using the printed loader object's suffix as data to memorize.
Criterion and solution. In the verified environment, ArrayList is defined by bootstrap, represented by null in this API. Printed internal names and loader identities depend on the implementation and execution.
C21-P01 – Two errors, two requests
Task. Explain the different starting points of Class.forName("example.Missing") and an already compiled class B containing new A() but run after removal of A.class. Distinguish the type and nature of failure.
Criterion and solution. Explicit lookup may throw ClassNotFoundException, a checked exception. Use of a required bytecode reference may produce NoClassDefFoundError, an Error; the message may contain a ClassNotFoundException as its cause. Earlier compilation of B does not load A into the JVM that will execute the program.
C23-A01 – The vector remainder
Task. Compile VectorSum with javac --release 25 --add-modules jdk.incubator.vector, run it with the same module option and vary array length. Explain the purpose of loopBound and the final scalar loop.
Criterion and solution. Complete groups are processed by the vector loop; remaining elements by the scalar loop. Lane count is not fixed output to compare between computers. The incubator-module warning is expected; the API's presence does not make it stable.
C23-P01 – The final group's mask
Task. Run MaskedVectorSum with five elements. If the preferred species has four lanes, draw the second iteration's mask. Repeat the reasoning if it has eight lanes and compare the result with the program using a scalar tail.
Criterion and solution. With four lanes, the second iteration has only one valid lane; with eight lanes, even the first iteration has five valid lanes and three excluded ones. The numerical result is equal in both algorithms. Lane count is a property of the observed platform, rather than a value guaranteed by the book.
C24-A01 – Metadata available at runtime
Task. In the Reflection program, change the annotation retention from RUNTIME to CLASS in a copy. Before running, predict the result of getAnnotation(Label.class) and the first instruction that cannot proceed.
Criterion and solution. Lookup returns null; the subsequent call to label.value() produces a NullPointerException. The robust solution checks null or retains RUNTIME if the contract requires reading during execution.
C24-P01 – Public and declared
Task. Add a private method to the Book record. Compare the questions asked by getMethod and getDeclaredMethod, without assuming that obtaining the descriptor automatically grants the right to invoke the method. Write how the problem would change in a non-open named module.
Criterion and solution. getMethod concerns visible public methods; getDeclaredMethod finds a directly declared method, even a private one. Invocation access follows separate rules and module encapsulation may prevent deep access. Perform the test in a test project, without indiscriminately opening packages.