package redis;

 import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class JedisPoolUtil {
private static volatile JedisPool jedisPool = null;
private JedisPoolUtil() {}
public static JedisPool getJedisPoolIntence() {
if(null == jedisPool) {
synchronized (JedisPoolUtil.class) {
if(null == jedisPool) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxActive(1000);
poolConfig.setMaxIdle(32);
poolConfig.setMaxWait(100*1000);
poolConfig.setTestOnBorrow(true); jedisPool = new JedisPool(poolConfig, "192.168.88.128", 6379);
return jedisPool;
}
}
}
return jedisPool;
} public static void relace(JedisPool jedisPool, Jedis jedis) {
if(null != jedis) {
jedisPool.returnResourceObject(jedis);
}
}
}
 package redis;

 import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class TestPool {
public static void main(String[] args) {
JedisPool jedisPool = JedisPoolUtil.getJedisPoolIntence();
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set("aa", "bb");
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}finally {
JedisPoolUtil.relace(jedisPool, jedis);
}
}
}

双锁单例

05-11 18:30