Grubhub interview question

What does the "volatile" keyword in Java indicate?

Interview Answer

Anonymous

Sep 24, 2016

1. Volatile variables are stored in the RAM. Every read and write operations happens in the RAM and not in CPU cache. 2. For multi-threaded application, each thread copies java variables from main memory to the CPU cache while working on them. Thus any changes made by one thread may not reflect to other threads as the updates are made on the local copies in the CPU cache. If the variable is declared volatile, only a single copy is created and changes are immediately reflected to all the threads accessing the variable. In short, Volatile keyword guarantees visibility across threads.