private final String RedisLockKey = "RedLock"; private final long altTimeout = 1 * 60 * 60 * 1000L;// 1hour /** * 獲取redis分布式鎖 * * @author Administrator * @version 1.0 * @return true:獲得鎖權限 false:未獲得鎖權限 */ private synchronized boolean getRedLock() { Long flag = jedis.setnx(RedisLockKey, String.valueOf(System.currentTimeMillis() + altTimeout + 1L)); if (flag == 0L) { String value = jedis.get(RedisLockKey); long longVal = Long.parseLong(value); if (longVal > System.currentTimeMillis()) {// 未過期 return Boolean.FALSE; } else {// 過期 String _value = jedis.getSet(RedisLockKey, String.valueOf(System.currentTimeMillis() + altTimeout + 1L)); long _longVal = Long.parseLong(_value); if (_longVal > System.currentTimeMillis()) { return Boolean.FALSE; } else { return Boolean.TRUE; } } } else { return Boolean.TRUE; } }