package utils; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.zxiaofan.util.rpc.RedisUtil; import junit.framework.TestCase; /** * * @author zxiaofan */ public class RedisUtilTest extends TestCase { String keyR = "key1"; // 对æå®keyæä½ int num = 500; // å¤çº¿ç¨èªå¢æ¬¡æ° Runnable runnable = new Runnable() { @Override public void run() { try { incr(keyR); } catch (Exception e) { e.printStackTrace(); } } }; /** * å¹¶åæµè¯ï¼å¤çº¿ç¨å¤åä¸keyèªå¢ï¼. * * è¯æï¼Redisæ¯å线ç¨çï¼å¯¹redisçæä½é½æ¯å ·æååæ§ç,æ¯çº¿ç¨å®å ¨çæä½ã * * @throws Exception */ public void testConcurrency() throws Exception { RedisUtil.set(keyR, "0"); // System.out.print(RedisUtil.getValue(keyR) + "-->"); ExecutorService service = Executors.newFixedThreadPool(5); for (int i = 0; i < num; i++) { service.execute(runnable); } service.shutdown(); while (!service.isTerminated()) { Thread.sleep(1000); } assertEquals("" + num, RedisUtil.get(keyR)); } /** * 循ç¯èªå¢ï¼å¡å¿ å éï¼. * * @param key * èªå¢key * @throws Exception * å¼å¸¸ */ private synchronized static void incr(String key) throws Exception { RedisUtil.set(key, String.valueOf(Integer.valueOf(RedisUtil.get(key)) + 1)); } }