Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real life use and explanation of the AtomicLongFieldUpdate class

Is anybody aware of any real life use of the class AtomicLongFieldUpdate? I have read the description but I have not quite grasped the meaning of it. Why do I want to know that? Curiosity and for OCPJP preparation.

Thanks in advance.

like image 335
Rollerball Avatar asked Jun 21 '13 16:06

Rollerball


1 Answers

You can think of a cost ladder for the following:

  • ordinary long: cheap, but unsafe for multi-threaded access
  • volatile long: more expensive, safe for multi-threaded access, atomic operations not possible
  • AtomicLong: most expensive, safe for multi-threaded access, atomic operations possible

(When I say 'unsafe' or 'not possible' I mean 'without an external mechanism like synchronization' of course.)

In the case where multi-threaded access is needed, but most operations are simple reads or writes, with only a few atomic operations needed, you can create one static instance of AtomicLongFieldUpdate and use this when atomic updates are needed. The memory/runtime overhead is then similar to a simple volatile variable, except for the atomic operations which are of the order of (or slightly more expensive than) the ordinary AtomicLong operations.

Here is a nice little tutorial.

like image 63
rxg Avatar answered Sep 24 '22 05:09

rxg