[ 
https://issues.apache.org/jira/browse/ZOOKEEPER-955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12969365#action_12969365
 ] 

Thomas Koch commented on ZOOKEEPER-955:
---------------------------------------

I'm myself +0 on this patch after what I've learned about synchronization now. 
It was just a WTF moment when I saw this code and asked myself why the author 
didn't use Atomic classes from the beginning. The comment you mentioned ("This 
should be called from a synchronized block on this!") is obsolete, I believe 
since the function is already synchronized.

> Use Atomic(Integer|Long) for (Z)Xid
> -----------------------------------
>
>                 Key: ZOOKEEPER-955
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-955
>             Project: ZooKeeper
>          Issue Type: Improvement
>          Components: java client, server
>            Reporter: Thomas Koch
>            Assignee: Thomas Koch
>            Priority: Trivial
>         Attachments: ZOOKEEPER-955.patch
>
>
> As I've read last weekend in the fantastic book "Clean Code", it'd be much 
> faster to use AtomicInteger or AtomicLong instead of synchronization blocks 
> around each access to an int or long.
> The key difference is, that a synchronization block will in any case acquire 
> and release a lock. The atomic classes use "optimistic locking", a CPU 
> operation that only changes a value if it still has not changed since the 
> last read.
> In most cases the value has not changed since the last visit so the operation 
> is just as fast as a normal operation. If it had changed, then we read again 
> and try to change again.
> [1] Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin) 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to