The Java Volatile Keyword Flashcards

1
Q

What is the Java Volatile Keyword used for?

A

It is used to guarantee visibility of changes to variables across threads

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does it mean to declare a variable as Volatile to the JVM?

A

By declaring the a variable volatile, all writes to the variable will be written back to main memory immediately.
Also, all reads of the variable will be read directly from main memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does it mean to declare a variable as Synchronised to the JVM?

A

Synchronised basically adds a critical flag to a specific variable. Thus, the JVM will know to block all but one write request to the variable at a time. It will, however, allow infinitely many simultaneous read requests - as long as no write request is currently occurring.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When should you use Volatile

A

Use Volatile when your variables are going to get read by multiple threads, but written to by only one thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When should you use Synchronised?

A

Use Synchronised when your variables will get read and written to by multiple
threads

How well did you know this?
1
Not at all
2
3
4
5
Perfectly