在分析这三种分布式锁实现之前,我们需要明确一点,分布式锁主要用于在分布式系统中控制同一资源被多个节点同时操作。
- 使用Redis的setnx命令实现分布式锁
 
Redis的setnx命令可以设置一个键,当且仅当这个键不存在的时候。我们可以利用这个特性来实现分布式锁。
public boolean lock(String key, String uniqueId, int expireTime) {
    long result = jedis.setnx(key, uniqueId);
    if (result == 1) {
        jedis.expire(key, expireTime);
        return true;
    }
    return false;
}
 
public void unlock(String key, String uniqueId) {
    if (uniqueId.equals(jedis.get(key))) {
        jedis.del(key);
    }
}
- 使用Java的synchronized关键字实现线程安全
 
在Java中,我们可以使用synchronized关键字来实现线程安全。
public synchronized void criticalSection() {
    // 需要线程安全保护的代码
}
- 使用RedLock算法实现分布式锁
 
RedLock算法是Redlock-redis实现分布式锁的一种协议。它是一种更为复杂的分布式锁解决方案,通过使用N个独立的Redis节点来实现锁。
public boolean lock(List<Jedis> nodes, String resourceId, long ttl) {
    int quorum = nodes.size() / 2 + 1;
    long time = System.currentTimeMillis();
    long validityTime = time + ttl + 1;
    String lockKey = "lock:" + resourceId;
    String lockValue = String.valueOf(validityTime);
 
    int lockAcquired = 0;
 
    for (Jedis node : nodes) {
        if (node.setnx(lockKey, lockValue) == 1) {
            node.pexpire(lockKey, ttl);
            lockAcquired++;
        } else {
            long lockValidity = Long.parseLong(node.get(lockKey));
            if (lockValidity < time) {
                String oldLockValue = node.getSet(lockKey, lockValue);
                if (oldLockValue != null && Long.parseLong(oldLockValue) < time) {
                    node.pexpire(lockKey, ttl);
                    lockAcquired++;
                }
            }
        }
    }
 
    return lockAcquired >= quorum;
}
 
public void unlock(List<Jedis> nodes, String resourceId) {
    String lockKey = "lock:" + resourceId;
    for (Jedis node : nodes) {
        node.del(lockKey);
    }
}
以上三种方法各有优缺点,具体使用哪种方法需要根据实际情况来决定。例如,如果你的应用是单机应用,那么使用synchronized就足够了。如果你的应用是分布式应用,那么可能需要使用Redlock或者Redis的setnx命令来实现分布式锁。而Redis的setnx命令实现的分布式锁简单易用,但是如果Redis服务器宕机,那么所有的节点都将无法获得锁。而Redlock算法虽然复杂,但是能够提供更好的可靠性和安全性。