(转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例
1. 源程序
- package com.sinosuperman.memcached;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import net.spy.memcached.CASResponse;
- import net.spy.memcached.CASValue;
- import net.spy.memcached.MemcachedClient;
- public class Test {
- private static MemcachedClient client = null;
- static {
- try {
- client = new MemcachedClient(new InetSocketAddress("localhost", 11211));
- } catch (IOException o) {
- }
- }
- public static void main(String[] args) throws Exception {
- client.set("numberss", 1800, 1);
- Test testObj = new Test();
- for (int i = 0; i < 10; i++) {
- testObj.new ThreadTest("Thread-" + (i + 1)).start();
- }
- }
- private class ThreadTest extends Thread {
- private MemcachedClient client = null;
- ThreadTest(String name) throws IOException {
- super(name);
- client = new MemcachedClient(new InetSocketAddress("localhost", 11211));
- }
- public void run() {
- int i = 0;
- int success = 0;
- while (i < 10) {
- i++;
- CASValue<Object> uniqueValue =client.gets("numberss");
- CASResponse response = client.cas("numberss",
- uniqueValue.getCas(), (Integer)uniqueValue.getValue() + 1);
- if (response.toString().equals("OK")) {
- success++;
- }
- if (i == 10)
- System.out.println(Thread.currentThread().getName() + " " + i
- + " time " + " update oldValue : " + uniqueValue.getValue()
- + " , result : " + response);
- }
- if (success < 10) {
- Count.incr(10 - success);
- System.out.println("Test counter: " + Count.get());
- }
- }
- }
- public static class Count {
- private static int counter = 0;
- public static void incr(int x) {
- counter += x;
- }
- public static int get() {
- return counter;
- }
- }
- }
2. 输出结果:
- Thread-1 10 time update oldValue : 14 , result : EXISTS
- Test counter: 6
- Thread-2 10 time update oldValue : 14 , result : EXISTS
- Test counter: 15
- Thread-3 10 time update oldValue : 17 , result : EXISTS
- Test counter: 22
- Thread-5 10 time update oldValue : 17 , result : EXISTS
- Test counter: 27
- Thread-4 10 time update oldValue : 20 , result : OK
- Test counter: 33
- Thread-8 10 time update oldValue : 27 , result : OK
- Test counter: 36
- Thread-6 10 time update oldValue : 28 , result : EXISTS
- Test counter: 43
- Thread-10 10 time update oldValue : 31 , result : EXISTS
- Test counter: 52
- Thread-9 10 time update oldValue : 31 , result : OK
- Test counter: 60
- Thread-7 10 time update oldValue : 35 , result : OK
- Test counter: 65
3. 分析
我们可以看到,未成功的次数最终为65,数据值最终为35,两者的和刚好是100,正好符合我们的实验结果预期。
(转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例的更多相关文章
- (转)实战Memcached缓存系统(1)Memcached基础及示例程序
1.Cache定义 (1)狭义概念:用于CPU的相对高速处理与主存(Main Memory)的相对低速处理的之间起到协调功能的硬件设备. (2)广义概念:用于速度相差较大的两种硬件之间,起到协调两者数 ...
- 6.memcached缓存系统
1.memcached的安装和参数 memcached缓存系统一般还是部署在linux服务器上,所以这里只介绍linux上memcache的安装 首先切换到root用户,然后apt-get insta ...
- Linux下搭建Memcached缓存系统
首先说下抱歉,博主近期单位经常加班.博客更新有点慢.希望大家理解,草稿箱里存了不少内容,等不忙时候一点点填坑~ 在一般的站点开发学习时候.都会把数据存放在RDBMS(关系型数据库系统(Relation ...
- Memcached 缓存系统简介
memcached官网:http://memcached.org/ What is Memcached? Memcached是一个自由开源的,高性能,高并发,分布式内存对象缓存系统. Memcache ...
- (转)实战Memcached缓存系统(5)Memcached的CAS程序实例
1. 非CAS 首先看一个不是CAS的Memcached程序实例.实例的问题原型,见上一篇博文. 程序实例: package com.sinosuperman.memcached; import ja ...
- (转)实战Memcached缓存系统(7)Memcached的一些基础FAQ
1. Memcached是什么? Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么? Memcached是基于Key/Value对的HashMap.每一对, ...
- (转)实战Memcached缓存系统(3)Memcached配置参数初解
一.基本参数 在我们第一次安装Memcached时,一般都是用过这个命令: memcached -m 512 -u root -d -l 127.0.0.1 -p 11211 我们先来解释这几个参数的 ...
- django之memcached缓存系统
django其他缓存方法:(https://www.cnblogs.com/jishuweiwang/p/6110809.html) memcached版本 <1.5 1. memcached缓 ...
- (转)实战Memcached缓存系统(4)Memcached的CAS协议
1. 什么是CAS协议 很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS ...
随机推荐
- Python 类型的分类
1.存储模型,对象可以保存多少个值.如果只能保存一个值,是原子类型.如果可以保存多个值,是容器类型.数值是原子类型,元组,列表,字典是容器类型.考虑字符串,按道理,字符串应该是容器类型,因为它包含多个 ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- osg通过glsl实现一个平面的水效果(法线贴图) 【转】
转自 http://blog.sina.com.cn/s/blog_78ea87380101ehk3.html 此文实现一个简单的的水面效果,主要是法线贴图, 效果图如下: 此文分为三部分:ver ...
- iOS常用的存储方式介绍
在iOS App开发过程中经常需要操作一些需要持续性保留的数据,比如用户对于App的相关设置.需要在本地缓存的数据等等.本文针对OC中经常使用的一下存储方式做了个整理. 常用的存储工具/方式: NSU ...
- iOS开发——动画篇Swift篇&炫酷弹出菜单
炫酷弹出菜单 这个是一个第三方按钮菜单组件,原版是使用Objective-C编写的名为AwesomeMenu的组件,地址是:https://github.com/levey/AwesomeMenu ...
- 图解JS原型链
一:任何一个对象都有一个prototype的属性,在js中可以把它记为:__proto__ 当初ECMAscript的发明者为了简化这门语言,同时又保持继承的属性,于是就设计了这个链表.. 在数据结构 ...
- StarlingMVC Framework 原理。。。
向starlingmvc 中添加bean后..会根据Metadata标签,分别交给不同的Processor去处理...然后会执行每个bean的postConstruct函数.相当于初始化函数...可以 ...
- java面试笔试谈
例一: public class Inc { public static void main(String[] args) { Inc inc=new Inc(); int i=5; inc.ferm ...
- APP测试基本流程
一. 测试周期 测试周期一般为两周,根据项目情况以及版本质量可适当缩短或延长测试时间.正式测试前先向主管或产品经理确认项目排期. 二.测试资源 测试任务开始前,检查各项测试资源. 产品功能需求文档 产 ...
- iOS之Xcode8 Auto Layout新特性
目录 1.Incrementally Adopting Auto Layout 2.Design and Runtime Constraints 3.NSGridView 4.Layout Feedb ...