Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use AtomicBoolean?  RSS feed

 
lil Yaure
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I be AtomicBoolean if they are likely to be changed by multiple threads constantly?
I'm already using AtomicInteger just to feel safe, but can't seem to find much on boolean.
When should I be using AtomicBoolean?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Most developers don't code using optimistic locking techniques. The reasoning are they generally don't know the techniques, it is very hard to get right, and standard locking/synchronization is all most books talk about.

Hence, the atomic libraries are generally not used very much.


In the case of AtomicInteger, the common methods used are set, get, and increment/decrement -- of which, only the later can't be done atomically using volatile variables. Most developers don't bother using the CAS method, which is the basis for optimistic locking.

In the case of AtomicBoolean, you have the CAS method, which is, again, the basis for optimistic locking. However, the common methods are the get and set, both of which can be done via volatile variables.

So, yea. For most developers, the AtomicBoolean class doesn't seem to be very useful over volatile variables.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!