Jedisスレッドプールのコード


メモ:(ネイティブi 5のテストデータ:redisスレッド実行時間:21187 ms)
public class Demo2 {
	
	private static JedisPool pool;
	
	static {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(100);
		config.setMaxIdle(20);
		config.setMaxWait(1000);
		config.setTestOnBorrow(true);
		
		pool = new JedisPool(config, "localhost");
	}

	public static void main(String[] args) {
		Demo2 demo = new Demo2();
		demo.test();
	}

	public void test() {
		initInsert();
		testThread();
	}
	
	private void initInsert() {
		Jedis jedis = pool.getResource();
		for(int i=0; i<20000; i++)
			jedis.set(String.valueOf(i), String.valueOf(i));
		pool.returnResource(jedis);
	}
	
	private void testThread() {
		long begin = System.currentTimeMillis();
		
		Thread thread[] = new Thread[60];
		for(int i=0; i<thread.length; i++) {
			thread[i] = new MyThread();
			thread[i].start();
		}
		
		for(int i=0; i<thread.length; i++) {
			try {
				thread[i].join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		long end = System.currentTimeMillis();
		System.out.println("       :" + (end - begin) + " ms");
	}
	
	class MyThread extends Thread {
		@Override
		public void run() {
			Jedis jedis = Demo2.pool.getResource();
			for(int i=0; i<20000; i++)
				jedis.get(String.valueOf(i));
			Demo2.pool.returnResource(jedis);
		}
	}
}