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

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值