Stephan van Hulst wrote:
Volatile establishes a happens-before relationship between executions of statements the same way synchronized does (referring to reordering of statements and writing and reading from main memory), but its main drawback is that it can't make execution of multiple operations atomic.
That's why the volatile keyword is almost completely useless.
The only common use case I've found for it is when the variable represents a very simple flag to cancel an operation that's in progress, or to flag that an operation has started or finished.
Stephan van Hulst wrote:
Paul Anilprem wrote:volatile is kind of a cheaper alternative to synchronization
The volatile is not an alternative, because it can't do everything that synchronized does.
Zhenyang Hua wrote:On Page 688/689, the book says the race condition could be fixed making getInstance() method synchronized AND the INSTANCE variable volatile:
Then it gives the explanation that the volatile makes sure the check null and create new Show() two steps are considered atomically one step (as they have the same visibility in the main memory by marking them volatile my guess), but is it really needed to use volatile since the getInstance() method is marked synchronized already? Wouldn't the synchronizing block other threads while it is executing so it is already considered thread-safe?
SCJA Study Guide wrote:We strongly recommend the SCJA certification to the following candidates:
* Entry-level and junior programmers wishing to start and/or continue down the path of using Java technologies.
* Software developers and technical leads wishing to solidify their Java-related skillsets.
* Project and program managers wishing to gain a true perspective of the work and challenge of their teams complementary to their planning and direction.
* Computer Science and information system students wishing to complement their education
* IT job seekers looking for a well-deserved edge
* Certification seekers wishing to round out their resume or curriculum vitae
It will not compile if you make this change because it is illegal to invoke a static method of an interface using a reference variable. If you want to invoke a static method of an interface, you need to use the name of the interface. For example, Pow.wow(); You can use a reference variable to invoke a static method of a class or super class though. So this is valid: ((Wow)f).wow(); even though it is not a good practice to use a reference to invoke a static method.