Prev Next

Java / volatile

1. volatile keyword.

volatile keyword indicates that a variable's value will be modified by different threads. When a java variable is declared volatile, The value of that varilable will not be cached thread-locally, all read/write will be directed to the "main memory"....

Read full answer

2. Difference between synchronized and volatile:

synchronized volatile Synchronized block applies only to object. volatile keyword can be applied to Object or primitive types....

Read full answer

3. Can an array be marked as volatile in Java?

Yes, however the volatility is limited to the reference pointing to an array and not the whole array or individual array elements.

Read full answer

4. Is reading a long/double variable is atomic?

long/double are 64-bit wide and it is read first 32 bit and next other 32 bits, so it is non-atomic.

Read full answer

5. Does long/double volatile variable is atomic?

yes, reading/writing a volatile long/double variable is atomic.

Read full answer

6. Practical uses of volatile modifier.

enable reading/writing into double/long as atomic. volatile variable enables Java memory model to have memory barrier so that when write is performed on a volatile variable it is guaranteed that any thread accessing that thread will see the value written.

Read full answer

7. What guarantee volatile variable provides? (OR) The Java volatile Happens-Before Guarantee

Since Java 5 the volatile keyword guarantees more than just the reading from and writing to main memory of variables. If Thread A writes to a volatile variable and Thread B subsequently reads the same volatile variable, then all variables...

Read full answer

8. volatile example.

package com.tutorials.volatileKeyword.volatileExample; public class MyThread { private static volatile int Shared_Variable = 0 ; public static void main(String[] args) { new ReadOnChange().start(); new Writer().start(); } static class ReadOnChange extends Thread { @ Override public void run() { i

Read full answer

9. Can a primitive variable be volatile in Java?

Yes. A primitive variable can be volatile as well as objects.

Read full answer

10. Main difference between locking and volatile variables.

Locking guarantee both visibility and atomicity while volatile variable can guarantee only the visibility.

Read full answer

11. How compiler and runtime treats a volatile variable?

Volatile variable means it is shared so the operations on it should not be reordered with other memory operations. These variables are not to be cached in registers or in other caches so that a read on volatile Caroline always...

Read full answer

12. Can a volatile variable be marked as final?

No. Java does not allow that because final variable value cannot be changed once initialized.

Read full answer

«
»

Comments & Discussions