When I executed the code from question 22, I got the following output:
And the supposedly correct answer is B:
The method consistently prints 100 100.
(Boyarsky, Jeanne; Selikoff, Scott. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (p. 909). Wiley. Kindle Edition.)
Perhaps I don't understand the meaning of the word consistently, my Kindle tells me that it is: in every case or on every occasion.
But the output is only displayed once, it is not repeated, so "consistently" is confusing here.
Also, the question is not stating that the code is executed more than once, and in other review questions that always meant it is executed once.
Assuming 100 milliseconds is enough time for the tasks submitted to the service executor to complete, what is the result of executing the following method? (Choose all that apply.)
A The method consistently prints 100 99.
B The method consistently prints 100 100.
C The output cannot be determined ahead of time.
D The code will not compile because of line w1.
E The code will not compile because of line w2.
F The code will not compile because of line w3.
G It compiles but throws an exception at runtime.
Yes, I guess you could interpret it like that, irrespective of how many times you run the program, meaning the code is executed more than once (every time as part of another process).
Single thread executor? Doesn't that mean you are running a single thread, so there won't be any synchronisation issues? You can see that both the int and the AtomicInteger object are always incremented 100×. You will see that the shutdown() method allows all tasks to complete.