Volatile keyword


What does the volatile keyword in Java do? For years I thought the keyword was quite obsolete, synchronization of values between the memory multiple processors seemed quite exotic. Since even modern-day processors have multiple cores, the keyword has become more relevant. A few months ago I saw a good presentation by Peter Veentjer on NLJug, that you can see online here. I can recommend the presentation.
Peter Veentjer also hinted about the Java Transaction Memory model. He was going to present on this subject on JSpring. Unfortunately I couldn’t attend the presentation myself, but I guess the presentation will be online soon as well.

While searching for more information (that I could copy&paste) on the volatile keyword, I found the following excellent serie of articles on site called JavaMex: The volatile keyword in Java. I hadn’t heard of the site before, from what I have read so far, the site has some excellent content. Amazingly what fine information you can find online for free!

2 Replies to “Volatile keyword”

  1. I don’t know how volatile is regarded in the Java world, but in .NET it is considered harmful by some. Not in the least because most people are uncertain whether it’s enough to use a volatile variable, and when to use a memory barrier.

    Unfortunately the link to the presentation by Peter Veentjer seems to be dead.

  2. Hello Marcel,

    Great to hear of you. In the Java world using volatile (assuming the keyword has the same meaning), as well as writing multi-threaded code in a direct way (which you’re doing then) is by many considered something you should do as little as possible by some.

    Many however still do, I’ve seen quite some crappy java code where people wrote their own scheduler, tried to make code optimized for multicore, creating threads themselves. In the end they had badly working functionality that would have worked out of the box if they’d chosen an existing framework instead.

    I’ve fixed the link so you can view the presentation again. Fixed some spelling errors too.