raja singh kumar wrote:. . . How is this an example of volatile keyword when there is only a single thread being used? . . .[
To the
Java® Language Specification (=JLS); also read §17.4.5. Oddly enough the Java10 edition of the JLS doesn't seem to have an index; in the Java8 edition it also lists
this part under “volatile”. You will see that writes and reads to a volatille variable happen‑after previous reads and writes. The happens‑after relationship applies irrespective of ho wfew or many threads are running, but you will only notice it if you have multiple threads.