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