JavaOne

EnfranchisedMind's Adventures at JavaOne 
« Back to blog

Concurrency Gotchas 4

Volatile counters: "++"/"--" is not an atomic operation, so volatile is not sufficient. count++ is "read, write, read", so reads and writes will propagate, but will not be atomic
 
Composing atomic actions doesn't work: repeated calls to a thread-safe class aren't atomic, so multiple calls to a thread-safe class aren't thread safe. Slapping a synchronized lock on things is odd, because you're adding a different (distinct) lock from the thread-safe class itself. If your class uses synchronized methods, it locks on "this", which is available both inside and outside of the app, and that works. Better is to use encapsulated methods: ConcurrentHashMap.
 
Assigning 64 bit values is not atomic on 32 bit JVMs.

Loading mentions Retweet

Comments (0)

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter