Java Multi-Threading Question:
Download Job Interview Questions and Answers PDF
Explain volatile?
Answer:
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.
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.
Download Java Multi-Threading Interview Questions And Answers
PDF
Previous Question | Next Question |
What is the priority for Garbage collector thread? | What is daemon thread and how it differs from user thread? |