单例方法 class 在方法内部使用共享资源是否是线程安全的?

Method of singleton class is thread safe or not with shared resource being used inside method?

我有一个 Singleton class,它有一个获取锁的方法,所有线程在继续执行该方法之前调用该方法。并且根据针对 Redis 键 的值获取锁(如果该值为 1 获取锁,否则不是).

我不需要同步这个方法吗?

public long getLock(Item item) {
    ValueOperations ops = template.opsForValue();
    return ops.increment(item.getKey());
}

无论答案是什么(Yes/No),请说明原因。

由于redis的增量操作,该方法看起来是线程安全的。

Redis 增量操作本质上是原子的,并且一次执行一个 Redis 操作。