1. 源程序

  1. package com.sinosuperman.memcached;
  2. import java.io.IOException;
  3. import java.net.InetSocketAddress;
  4. import net.spy.memcached.CASResponse;
  5. import net.spy.memcached.CASValue;
  6. import net.spy.memcached.MemcachedClient;
  7. public class Test {
  8. private static MemcachedClient client = null;
  9. static {
  10. try {
  11. client = new MemcachedClient(new InetSocketAddress("localhost", 11211));
  12. } catch (IOException o) {
  13. }
  14. }
  15. public static void main(String[] args) throws Exception {
  16. client.set("numberss", 1800, 1);
  17. Test testObj = new Test();
  18. for (int i = 0; i < 10; i++) {
  19. testObj.new ThreadTest("Thread-" + (i + 1)).start();
  20. }
  21. }
  22. private class ThreadTest extends Thread {
  23. private  MemcachedClient client = null;
  24. ThreadTest(String name) throws IOException {
  25. super(name);
  26. client = new MemcachedClient(new InetSocketAddress("localhost", 11211));
  27. }
  28. public void run() {
  29. int i = 0;
  30. int success = 0;
  31. while (i < 10) {
  32. i++;
  33. CASValue<Object> uniqueValue =client.gets("numberss");
  34. CASResponse response = client.cas("numberss",
  35. uniqueValue.getCas(), (Integer)uniqueValue.getValue() + 1);
  36. if (response.toString().equals("OK")) {
  37. success++;
  38. }
  39. if (i == 10)
  40. System.out.println(Thread.currentThread().getName() + " " +  i
  41. + " time " + " update oldValue : " + uniqueValue.getValue()
  42. +  " , result : " + response);
  43. }
  44. if (success < 10) {
  45. Count.incr(10 - success);
  46. System.out.println("Test counter: " + Count.get());
  47. }
  48. }
  49. }
  50. public static class Count {
  51. private static int counter = 0;
  52. public static void incr(int x) {
  53. counter += x;
  54. }
  55. public static int get() {
  56. return counter;
  57. }
  58. }
  59. }

2. 输出结果:

  1. Thread-1 10 time  update oldValue : 14 , result : EXISTS
  2. Test counter: 6
  3. Thread-2 10 time  update oldValue : 14 , result : EXISTS
  4. Test counter: 15
  5. Thread-3 10 time  update oldValue : 17 , result : EXISTS
  6. Test counter: 22
  7. Thread-5 10 time  update oldValue : 17 , result : EXISTS
  8. Test counter: 27
  9. Thread-4 10 time  update oldValue : 20 , result : OK
  10. Test counter: 33
  11. Thread-8 10 time  update oldValue : 27 , result : OK
  12. Test counter: 36
  13. Thread-6 10 time  update oldValue : 28 , result : EXISTS
  14. Test counter: 43
  15. Thread-10 10 time  update oldValue : 31 , result : EXISTS
  16. Test counter: 52
  17. Thread-9 10 time  update oldValue : 31 , result : OK
  18. Test counter: 60
  19. Thread-7 10 time  update oldValue : 35 , result : OK
  20. Test counter: 65

3. 分析

我们可以看到,未成功的次数最终为65,数据值最终为35,两者的和刚好是100,正好符合我们的实验结果预期。

(转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例的更多相关文章

  1. (转)实战Memcached缓存系统(1)Memcached基础及示例程序

    1.Cache定义 (1)狭义概念:用于CPU的相对高速处理与主存(Main Memory)的相对低速处理的之间起到协调功能的硬件设备. (2)广义概念:用于速度相差较大的两种硬件之间,起到协调两者数 ...

  2. 6.memcached缓存系统

    1.memcached的安装和参数 memcached缓存系统一般还是部署在linux服务器上,所以这里只介绍linux上memcache的安装 首先切换到root用户,然后apt-get insta ...

  3. Linux下搭建Memcached缓存系统

    首先说下抱歉,博主近期单位经常加班.博客更新有点慢.希望大家理解,草稿箱里存了不少内容,等不忙时候一点点填坑~ 在一般的站点开发学习时候.都会把数据存放在RDBMS(关系型数据库系统(Relation ...

  4. Memcached 缓存系统简介

    memcached官网:http://memcached.org/ What is Memcached? Memcached是一个自由开源的,高性能,高并发,分布式内存对象缓存系统. Memcache ...

  5. (转)实战Memcached缓存系统(5)Memcached的CAS程序实例

    1. 非CAS 首先看一个不是CAS的Memcached程序实例.实例的问题原型,见上一篇博文. 程序实例: package com.sinosuperman.memcached; import ja ...

  6. (转)实战Memcached缓存系统(7)Memcached的一些基础FAQ

    1. Memcached是什么? Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么? Memcached是基于Key/Value对的HashMap.每一对, ...

  7. (转)实战Memcached缓存系统(3)Memcached配置参数初解

    一.基本参数 在我们第一次安装Memcached时,一般都是用过这个命令: memcached -m 512 -u root -d -l 127.0.0.1 -p 11211 我们先来解释这几个参数的 ...

  8. django之memcached缓存系统

    django其他缓存方法:(https://www.cnblogs.com/jishuweiwang/p/6110809.html) memcached版本 <1.5 1. memcached缓 ...

  9. (转)实战Memcached缓存系统(4)Memcached的CAS协议

    1. 什么是CAS协议 很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS ...

随机推荐

  1. [置顶] 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/9825113 异步加载图片的例子,网上也比较多,大部分用了HashMap<Strin ...

  2. 如何将松散的dll打包进需要发布的exe

    我们需要发布的exe文件很多时候都可能会依赖于一堆松散的dll,如果想在发布的时候只提供exe文件,而不想把一大堆dll一起放在和exe同一个文件夹下,是有方法的,该方法由CLR via C#作者提出 ...

  3. 细看InnoDB数据落盘 图解 MYSQL 专家hatemysql

    http://hatemysql.com/?p=503 1.  概述 前面很多大侠都分享过MySQL的InnoDB存储引擎将数据刷新的各种情况.我们这篇文章从InnoDB往下,看看数据从InnoDB的 ...

  4. jQuery Mobile 连接外部连接或切换动画

    jQuery Mobile不同网页之间的跳转问题 jQuery Mobile,一个新的手机终端脚本开发库,从名字可以看出,它是基于jQuery:目前支持很多种手机设备,包括IOS/Android/Bl ...

  5. Linux shell 脚本攻略之比较与测试

    摘自:<Linux shell 脚本攻略>Page30-33

  6. android 几个小技巧

    1.如果打开模拟器,不同程序打开了不同的模拟器.可能是某个某个模拟器的target版本过低,修改一下4.2,应该都可以用了 2.找不到R.id.的错误,不妨删除menu文件夹下的xml文件 3.act ...

  7. iOS 8 CloudKit上手教程

    CloudKit,是苹果最新推出的基于iCloud的一个云端数据存储服务,提供了低成本的云存储并能作为一个后端服务通过用户们的iCloud账号分享其应用数据. CloudKit主要由两个部分组成: 一 ...

  8. Jfinal学习笔记

    实习期间,公司使用jfinal做开发,所以就学习了这个框架 Jfinal作为一个极速ORM,使用上非常方便,核心源代码1万多行,压缩后只有200多KB. 从入口开始 作为一个web项目,首先解读web ...

  9. web2.0、互联网+、IT时代与DT时代、工业4.0 引发的思考

    最近忙着找实习,来学校一个星期还没到,就感觉已经经历了几个春秋. 第一个实习面试是个杭州互联网小公司,面WEB前端开发实习,怪我一个暑假两个月一点书都没碰,偏偏赶上G20到9/9才开学,没啥准备就一头 ...

  10. 不支持关键字:metadata

    将 string sqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Cos ...