十四、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/ 在这篇文章,我将深入研究 ...
随机推荐
- datatime模块
https://www.cnblogs.com/cindy-cindy/p/6720196.html
- [bzoj4444] 国旗计划 双指针+倍增
Description A国正在开展一项伟大的计划--国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此,国土安全局已经挑选了N名优秀的边 ...
- AcDbTable表格实体的简单例子
例子是创建一个含有表格实体的块定义 效果如下(手动插入的块) 源代码如下,简单示意,采用了我不是很熟悉的智能指针创建实体对象,代码仅供参考 AcDbObjectPointer<AcDbTable ...
- PHP错误——Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)
解释是可用内存已耗尽,这关系到PHP的memory_limit的设置问题. 这里有两种方法解决 1.修改php.ini memory_limit = 128 打开终端输入下列bash命令 cd /pr ...
- Java文件字节流和字符流
输入流:只能从中读取数据,不能向其写入数据. InputStream,Reader 输出流:只能向其中写入数据,不能从中读取数据. OutputStream, Writer 输入流是相对于程序而言,外 ...
- ADC新库
1.单次采集模式 1.在STM32CUBMX中设置为单次采集模式 2.在C文件中用HAL_ADC_START()函数启动ADC 3.用HAL_ADC_PollForConversion()延时等待采集 ...
- 【算法笔记】A1063 Set Similarity
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- windows下python管理右键菜单
实现很简单,不记得什么时候写的了,贴出来希望能有所价值 """ Windows中创建右键菜单 """ import os import sy ...
- [转] Actor生命周期理解
[转] https://blog.csdn.net/wsscy2004/article/details/38875065 镇图:Actor内功心法图 Actor的生命周期可以用Hooks体现和控制,下 ...
- 用js制作简易计算器及猜随机数字游戏
<!doctype html><html><head> <meta charset="utf-8"> <title>JS ...