十四、curator recipes之DistributedAtomicLong
简介
和Java的AtomicLong没有太大的不同DistributedAtomicLong旨在分布式场景中维护一个Long类型的数据,你可以像普通单机环境一样来使用它。
官方文档:http://curator.apache.org/curator-recipes/distributed-atomic-long.html
代码示例
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicLong;
import org.apache.curator.retry.ExponentialBackoffRetry; public class DistributedAtomicLongDemo {
private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 2));
private static String path = "/atomic/long/0001";
private static DistributedAtomicLong atomicLong = new DistributedAtomicLong(client, path, new ExponentialBackoffRetry(1000, 1));
static {
client.start();
} public static void main(String[] args) throws Exception {
System.out.println(atomicLong.get().postValue());
atomicLong.increment();
System.out.println(atomicLong.get().postValue());
atomicLong.decrement();
System.out.println(atomicLong.get().postValue());
AtomicValue<Long> newAtomicLong = atomicLong.compareAndSet(0L, 10L);
System.out.println(newAtomicLong.succeeded());
System.out.println(newAtomicLong.preValue());
System.out.println(newAtomicLong.postValue());
System.out.println(atomicLong.get().postValue());
System.out.println(atomicLong.subtract(5L).postValue());
Thread.sleep(50000);
client.close();
}
}
十四、curator recipes之DistributedAtomicLong的更多相关文章
- 二十、curator recipes之NodeCache
简介 Curator的NodeCache允许你监听一个节点,当节点数据更改或者节点被删除的时候将会触发监听. 官方文档:http://curator.apache.org/curator-recipe ...
- 十、curator recipes之信号量InterProcessSemaphoreV2
简介 跟Java并信号量没有什么不同,curator实现的信号量也是基于令牌桶算法,当一个线程要执行的时候就去桶里面获取令牌,如果有足够的令牌那么我就执行如果没有那么我就阻塞,当线程执行完毕也要将令牌 ...
- 我的MYSQL学习心得(十四) 备份和恢复
我的MYSQL学习心得(十四) 备份和恢复 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) ...
- 雅虎(yahoo)前端优化十四条军规
第一条.尽可能的减少 HTTP 的请求数 (Make Fewer HTTP Requests ) http请求是要开销的,想办法减少请求数自然可以提高网页速度.常用的方法,合并css,js(将一个页面 ...
- Bootstrap<基础二十四> 缩略图
Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...
- Bootstrap<基础十四> 按钮下拉菜单
使用 Bootstrap class 向按钮添加下拉菜单.如需向按钮添加下拉菜单,只需要简单地在在一个 .btn-group 中放置按钮和下拉菜单即可.也可以使用 <span class=&qu ...
- AngularJs的UI组件ui-Bootstrap分享(十四)——Carousel
Carousel指令是用于图片轮播的控件,引入ngTouch模块后可以在移动端使用滑动的方式使用轮播控件. <!DOCTYPE html> <html ng-app="ui ...
- C#编程总结(十四)dynamic
http://www.cnblogs.com/yank/p/4177619.html C#编程总结(十四)dynamic 介绍 Visual C# 2010 引入了一个新类型 dynamic. 该类型 ...
- 解剖SQLSERVER 第十四篇 Vardecimals 存储格式揭秘(译)
解剖SQLSERVER 第十四篇 Vardecimals 存储格式揭秘(译) http://improve.dk/how-are-vardecimals-stored/ 在这篇文章,我将深入研究 ...
随机推荐
- Mybatis 动态Sql语句《常用》
MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...
- PHP和JS中全局变量和局部变量
一,PHP中全局变量和局部变量 php与C++中对全局变量和局部变量定义类似,全局变量:函数外定义的变量,在全局通用:局部变量:在函数内定义的变量,只在函数内有效.PHP中变量范围跨越了include ...
- [javascript]—jQuery解析本地 XML 文档
Create a jQuery object using an XML string and obtain the value of the title node. <!doctype html ...
- springboot自定义配置文件
前言:如果你一点spring的基础没有,建议你不要学习springboot,至少先有一个spring的项目经验或者自己搭建过spring的项目再学习springboot,这样你会发现在spring中搞 ...
- [译文]casperjs的API-mouse模块
mouse类 这个类是对各种鼠标操作的抽象,比如移动,点击,双击,滚动等.它要求一个已经获得DOM属性的casper对象,能用这种方式创造一个鼠标对象: var casper = require(&q ...
- python --爬虫基础 --爬猫眼top 100 使用 requests 库的基本操作
import requests import re import json import time def get_page(url): # 获取页数 headers = { 'User-Agent' ...
- 3.1 High Availability
摘要: 出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该 ...
- .NET熔断之Polly
1. Net Core 中有一个被.Net 基金会认可的库 Polly,可以用来简化熔断降级的处理.主要功能:重试(Retry):断路器(Circuit-breaker):超时检测(Timeout): ...
- Array flat的实现
if (!Array.prototype.flat) { Array.prototype.flat = function (num = 1) { if (!Number(num) || Number( ...
- eclipse左边的项目栏消失的处理方法
window —–> Show View —–> other —–> package Explorer