实战Memcached缓存系统(6)Memcached CAS的多线程程序实例

来源:互联网 发布:linux开机启动tomcat 编辑:程序博客网 时间:2024/05/22 11:40
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"18001);  
  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,正好符合我们的实验结果预期。

0 0