curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jordan Zimmerman <jor...@jordanzimmerman.com>
Subject Re: atomic setCount() in SharedCount
Date Thu, 17 Oct 2013 14:45:31 GMT
No, that is not safe. It seems you are wanting to do an Atomic increment. In that case you
should use DistributedAtomicInteger instead of SharedCount.

-Jordan

On Oct 17, 2013, at 6:36 AM, Sznajder ForMailingList <bs4mailinglist@gmail.com> wrote:

> Hi,
> 
> In my configuration, several workers update a Sharedcount object by increment or decrement
the counter of the node.
> I implemented this by this piece of code:
> 
>     public void incrementForKey(String key) throws Exception {
>         SharedCount count = new SharedCount(this.framework,
>                 this.path + "/" + key,0);
>         count.start();
>         count.setCount(count.getCount()+1);
>     }
> 
> However, I am worrying about the last line:
>         count.setCount(count.getCount()+1);
> 
> What happens, in case, two workers concurrently, try to increment or decrement the node
value? I suppose this is not atomic and so, how can I solve that?
> 
> Benjamin.


Mime
View raw message