实战Memcached缓存系统(6)Memcached CAS的多线程程序实例
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 08:52 点击:次
1. 源程序
[java]
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)
&nbs
[java]
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)
&nbs
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>