Answers

Question and Answer:

  Home  Java Multi-Threading

⟩ Explain volatile?

When more than one threads access the same variable then each thread will maintain its own copy of that variable in the form of local cache.

When a thread is changing the value of variable, it is actually changing the local cache not the main variable memory.

Every thread which is using the same variable doesn’t know anything about the values changed by another thread.

To overcome this problem, if you declare a variable as volatile, then whenever a thread is changing the value of a volatile, it is actually changing to the main memory. So, all other threads which are accessing the same variable can get the changed value.

 140 views

More Questions for you: