|
|
Home
Synchronization and concurrency
wait/notify
final
volatile
synchronized keyword
Java threading
Deadlock (and avoiding it)
Java 5: ConcurrentHashMap
Atomic variables
Explicit locks
Queues
Semaphores
CountDownLatch
CyclicBarrier
Find what you were looking for?
See also:Related to volatile: The volatile keyword in JavaTopics covered include:
It's probably fair to say that on the whole, the volatile keyword in Java is poorly documented, poorly understood, and rarely used. To make matters worse, its formal definition actually changed as of Java 5. Essentially, volatile is used to indicate that a variable's value will be modified by different threads. Declaring a volatile Java variable means:
We say "acts as though" in the second point, because to the programmer at least (and probably in most JVM implementations) there is no actual lock object involved. Here is how synchronized and volatile compare:
In other words, the main differences between synchronized and volatile are:
Attempting to synchronize on a null object will throw a NullPointerException. Volatile variables in Java 5We mentioned that in Java 5, the meaning of volatile has been tightened up. We'll come back to this issue in a moment. First, we'll look at a typical example of using volatile. Later, we'll look at topics such as:
Copyright © Javamex UK 2012. All rights reserved. |