手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

实战Memcached缓存系统

Memcached CAS的多线程程序实例

[日期:2012-01-15] 来源:Linux社区  作者:poechant [字体:大 中 小]

1. 源程序

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

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,正好符合我们的实验结果预期。

linux
【内容导航】
第1页:Memcached基础及示例程序 第2页:Memcached Java API基础之MemcachedClient
第3页:Memcached配置参数初解 第4页:Memcached的CAS协议
第5页:Memcached的CAS程序实例 第6页:Memcached CAS的多线程程序实例
第7页:Memcached的一些基础FAQ 第8页:Memcached异步实时读写问题的解决方案SAC
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款