Memcached(六)Memcached的并发实例
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 TestCASMultiThread { private static MemcachedClient client = null; static {
try {
client = new MemcachedClient(new InetSocketAddress("localhost",
11211));
} catch (IOException o) {
}
} 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 void main(String[] args) throws Exception { client.set("numberss", 1800, 1); TestCASMultiThread testObj = new TestCASMultiThread();
for (int i = 0; i < 10; i++) {
testObj.new ThreadTest("Thread-" + (i + 1)).start();
}
} public static class Count {
private static int counter = 0; public synchronized static void incr(int x) {
counter += x;
} public static int get() {
return counter;
}
}
}
2016-02-25 16:12:12.495 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.498 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@fa9cf
2016-02-25 16:12:12.503 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.504 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@b179c3
2016-02-25 16:12:12.505 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.507 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@aa9835
2016-02-25 16:12:12.508 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.509 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@7b7072
2016-02-25 16:12:12.511 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.513 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@bfbdb0
2016-02-25 16:12:12.514 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.515 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@9fef6f
2016-02-25 16:12:12.516 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
Thread-1 10 time update oldValue : 12 , result : OK
Test counter: 5
Thread-3 10 time update oldValue : 15 , result : EXISTS
Test counter: 15
2016-02-25 16:12:12.518 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@f38798
2016-02-25 16:12:12.520 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.525 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@7a78d3
2016-02-25 16:12:12.525 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
Thread-5 10 time update oldValue : 17 , result : EXISTS
Test counter: 21
2016-02-25 16:12:12.525 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1a5ab41
2016-02-25 16:12:12.525 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.525 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@181afa3
2016-02-25 16:12:12.525 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-02-25 16:12:12.541 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@8ed465
Thread-7 10 time update oldValue : 26 , result : EXISTS
Test counter: 24
Thread-9 10 time update oldValue : 30 , result : OK
Test counter: 27
Thread-2 10 time update oldValue : 32 , result : OK
Test counter: 32
Thread-6 10 time update oldValue : 33 , result : OK
Test counter: 40
Thread-4 10 time update oldValue : 33 , result : EXISTS
Test counter: 48
Thread-8 10 time update oldValue : 38 , result : OK
Test counter: 52
Thread-10 10 time update oldValue : 40 , result : OK
Test counter: 60
Memcached(六)Memcached的并发实例的更多相关文章
- geotrellis使用(六)Scala并发(并行)编程
本文主要讲解Scala的并发(并行)编程,那么为什么题目概称geotrellis使用(六)呢,主要因为本系列讲解如何使用Geotrellis,具体前几篇博文已经介绍过了.我觉得干任何一件事情基础很重要 ...
- memcached—向memcached中保存Java实体需注意的问题
今天以代码实例的形式总结一下向memcached中保存Java实体需注意的问题: memcached工具类代码: package com.ghj.packageoftool; import java. ...
- memcached -- 运行memcached
Memcached 运行 Memcached命令的运行: $ /usr/local/memcached/bin/memcached -h 注意:如果使用自动安装, memcached 命令位于 /us ...
- 后端——框架——缓存框架——memcached——《Memcached教程》阅读笔记
Memcached的知识点大致可以分为三个部分. 服务器部分:环境搭建. 概念:存储的数据类型,指令,内存的替换策略. 集成:与Java语言的集成. 1.搭建环境 1.1 Linux环境 在Linux ...
- vue第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件)
第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件) #课程目标 掌握vue实例的相关属性和方法的含义和使用 了解vue的数据响应原理 熟悉创建组件,了解全局 ...
- Memcached(七)Memcached的并发实例
1. Memcached是什么?Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么?Memcached是基于Key/Value对的HashMap.每一对,都 ...
- Memcached(五)Memcached的并发实例
package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAddress; i ...
- memcached在windows下多实例并存
文章来源:http://blog.csdn.net/xingxing513234072/article/details/39343999 memcached.exe的-d install命令安装时其他 ...
- (转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例
1. 源程序 package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAdd ...
随机推荐
- angualrjs学习总结二(作用域、控制器、过滤器)
一:Scope简介 Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带.Scope 是一个对象,有可用的方法和属性.Scope 可应用在视图和控制器上. ...
- 关于python使用list出现乱码的解决
昨天在敲python的一个小实例的时候,用到了readlines()这个函数,但是将文件读出来的时候是乱码,也并不是完全乱码,只是中文出现了乱码,数字还是显示正常的,同时也不报错.源码以及文件截图如下 ...
- 关于java实现同步的方法
什么是线程同步? 当使用多个线程来访问同一个数据时,非常容易出现线程安全问题(比如多个线程都在操作同一数据导致数据不一致),所以我们用同步机制来解决这些问题. 实现同步机制有两个方法: 1. 同步代码 ...
- transition和animation动画简介
本文介绍CSS动画的两大组成部分:transition和animation.我不打算给出每一条属性的详尽介绍,那样可以写一本书.这篇文章只是一个简介,帮助初学者了解全貌,同时又是一个快速指南,当你想不 ...
- 第六章 jQuery操作表单
1.单行文本框的应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...
- Logcat中报内存泄漏MemoryLeak的一次分析
有时候运行APP的时候Logcat中会报错,提示资源没有释放,Memory leak, 于是打开Android Studio在Android Monitor工具栏点开,有Logcat和Monitors ...
- C#当中的泛型和java中的对比
1.C#中的泛型 先写一个Demo: namespace generic { public class Program { static ...
- MS SQLService中的*= 及 =*
今天看到数据库中的存储过程中,有*= ,于是百度了一下,原来这个SQL2000以前的左连接,以后的版本中开启兼容也是可以用的. 于是拿出来在系统中的表中测试了一下果然如此 例: 有用户表D_user及 ...
- XML文件的解析方式
XML文件4种解析方式分别是:DOM解析,SAX解析,JDOM解析,DOM4J解析.1.基础方法:DOM:与平台无关的官方的解析方式.SAX:Java平台提供的基于事件驱动的解析方式.2.扩展方法(在 ...
- Java创建线程的第二种方式:实现runable接口
/*需求:简单的卖票程序多个窗口买票 创建线程的第二种方式:实现runable接口 *//*步骤1.定义类实现Runable接口2.覆盖Runable接口中的run方法 将线程要运行的代码存放在 ...