實戰Memcached快取系統(6)Memcached CAS的多執行緒程式例項
1. 源程式
3. 分析
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,正好符合我們的實驗結果預期。
相關文章
- 實戰Memcached快取系統(5)Memcached的CAS程式例項快取
- 實戰Memcached快取系統(4)Memcached的CAS協議快取協議
- 實戰Memcached快取系統(1)Memcached基礎及示例程式快取
- 實戰Memcached快取系統(3)Memcached配置引數初解快取
- 實戰Memcached快取系統(7)Memcached的一些基礎FAQ快取
- 實戰Memcached快取系統(2)Memcached Java API基礎之MemcachedClient快取JavaAPIclient
- 實戰Memcached快取系統(8)Memcached非同步實時讀寫問題的解決方案SAC快取非同步
- 分散式快取系統之Memcached分散式快取
- Memcached 多執行緒和狀態機執行緒
- 快取: Memcached和terracotta快取
- Memcached::cas 詳解
- Memcached 分散式快取實現原理分散式快取
- 記憶體快取系統memcached與redis實現的對比記憶體快取Redis
- 伺服器後端開發系列——《實戰Memcached記憶體快取系統》伺服器後端記憶體快取
- memcached資料庫快取資料庫快取
- Memcached安裝與使用例項
- java多執行緒例項Java執行緒
- C#呼叫Couchbase中的Memcached快取C#快取
- memcached快取知識簡單梳理快取
- C#多執行緒程式設計例項C#執行緒程式設計
- Java多執行緒之CASJava執行緒
- memcached的執行狀態監控
- Qt中的多執行緒與執行緒池淺析+例項QT執行緒
- 如何利用memcached和wcf實現一個快取服務快取
- 分散式快取伺服器Memcached介紹分散式快取伺服器
- C#多執行緒程式設計實戰1.1建立執行緒C#執行緒程式設計
- 在IDEAL下執行測試Memcached快取資料功能的時候,快取時間過期之坑Idea快取
- 系統快取全解析7:第三方分散式快取解決方案 Memcached和Cacheman快取分散式
- 通過Cache::Memcached方式 例項物件應用物件
- pytest(13)-多執行緒、多程式執行用例執行緒
- Java多執行緒之守護執行緒實戰Java執行緒
- 實戰.Net多執行緒(一)執行緒
- 實戰.Net多執行緒(二)執行緒
- 實戰.Net多執行緒(三)執行緒
- 實戰.Net多執行緒(四)執行緒
- Shopify使用Memcached而不是Redis快取提升20%效能Redis快取
- Linux系統中部署memcachedLinux
- 實戰體會Java的多執行緒程式設計Java執行緒程式設計