• Curator使用:(六)分布式计数器


    分布式计数器介绍##

    和分布式锁类似,下面是实现分布式计数器的功能

    代码###

        //分布式计数器
        DistributedAtomicInteger atomicInteger = new DistributedAtomicInteger(cc,"/distributed_atomic_counter",new ExponentialBackoffRetry(1000,3));
        AtomicValue av = atomicInteger.add(10);
        System.out.println(av.succeeded());//true
        System.out.println(av.preValue());//0
        System.out.println(av.postValue());//10
    

    每个DistributedAtomicXXX里面都有一个AtomicValue,这个是分布式的核心实现类。

        AtomicValue<byte[]>   trySet(MakeValue makeValue) throws Exception
        {
            MutableAtomicValue<byte[]>  result = new MutableAtomicValue<byte[]>(null, null, false);
            //尝试下乐观锁
            tryOptimistic(result, makeValue);
            if ( !result.succeeded() && (mutex != null) )
            {
                //失败的话再使用排他锁
                tryWithMutex(result, makeValue);
            }
    
            return result;
        }
    
    
  • 相关阅读:
    RESTful API 设计原则
    c#的逆变和协变
    Java内部类之间的闭包和回调详解
    java的内部类
    抓包工具
    HashMap与HashTable的区别
    Java 语法清单
    Java面试问题列表
    bootstrap table api
    c# CacheManager 缓存管理
  • 原文地址:https://www.cnblogs.com/june777/p/11881340.html
Copyright © 2020-2023  润新知