import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class RedisLockTest {
public static int count = 0;
public static void main(String[] args) {
Config config = new Config();
config.useSingleServer().setAddress("redis://xxx.xxx.x.xxx:6379").setDatabase(0);
RedissonClient redisson = Redisson.create(config);
for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
@Override
public void run() {
RLock rLock = redisson.getLock("lockKey:aaaaaaaaaaaaaaaaaaaaa");
try {
boolean res = rLock.tryLock(10, 10, TimeUnit.SECONDS);
if (res) {
try {
Thread.sleep(1);
} catch (Exception e) {
}
count++;
System.out.println("运行结果:count=" + count);
}
} catch (Exception e) {
throw new RuntimeException("aquire lock fail");
} finally {
rLock.unlock();
}
}
}).start();
}
}
}