文章目录
使用Redisson演示可重入锁 读写锁 信号量 闭锁 获取三级分类redisson分布式锁
package com. xd. cubemall. product. config ;
import org. redisson. Redisson ;
import org. redisson. api. RedissonClient ;
import org. redisson. config. Config ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
public class MyRedissonConfig {
@Bean ( destroyMethod= "shutdown" )
RedissonClient redissonClient ( ) {
Config config = new Config ( ) ;
config. useSingleServer ( ) . setAddress ( "redis://127.0.0.1:6379" ) ;
RedissonClient redissonClient = Redisson . create ( config) ;
return redissonClient;
}
}
使用Redisson演示可重入锁
@ResponseBody
@GetMapping ( "/hello" )
public String hello ( ) {
RLock lock = redissonClient. getLock ( "my-lock" ) ;
lock. lock ( ) ;
try {
System . out. println ( "加锁成功,执行业务。。。" + Thread . currentThread ( ) . getId ( ) ) ;
Thread . sleep ( 10000 ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
System . out. println ( "解锁。。。" + Thread . currentThread ( ) . getId ( ) ) ;
lock. unlock ( ) ;
}
return "hello" ;
}
读写锁
@GetMapping ( "/write" )
@ResponseBody
public String writeValue ( ) {
RReadWriteLock lock = redissonClient. getReadWriteLock ( "rw-lock" ) ;
String s = "" ;
RLock writeLock = lock. writeLock ( ) ;
try {
writeLock. lock ( ) ;
System . out. println ( "写锁加锁成功..." + Thread . currentThread ( ) . getId ( ) ) ;
s = UUID . randomUUID ( ) . toString ( ) ;
Thread . sleep ( 15000 ) ;
redisTemplate. opsForValue ( ) . set ( "writeValue" , s) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
writeLock. unlock ( ) ;
System . out. println ( "写锁。。释放。。" + Thread . currentThread ( ) . getId ( ) ) ;
}
return s;
}
@GetMapping ( "/read" )
@ResponseBody
public String readValue ( ) {
RReadWriteLock lock = redissonClient. getReadWriteLock ( "rw-lock" ) ;
String s = "" ;
RLock readLock = lock. readLock ( ) ;
readLock. lock ( ) ;
try {
System . out. println ( "读锁...加锁成功..." + Thread . currentThread ( ) . getId ( ) ) ;
s = redisTemplate. opsForValue ( ) . get ( "writeValue" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
readLock. unlock ( ) ;
System . out. println ( "读锁。。释放。。。" + Thread . currentThread ( ) . getId ( ) ) ;
}
return s;
}
信号量
@GetMapping ( "/park" )
@ResponseBody
public String park ( ) throws InterruptedException {
RSemaphore semaphore = redissonClient. getSemaphore ( "park" ) ;
semaphore. acquire ( ) ;
return "ok" ;
}
@GetMapping ( "/go" )
@ResponseBody
public String go ( ) {
RSemaphore semaphore = redissonClient. getSemaphore ( "park" ) ;
semaphore. release ( ) ;
return "ok" ;
}
闭锁
@ResponseBody
@GetMapping ( "/lockDoor" )
public String lockDoor ( ) throws InterruptedException {
RCountDownLatch door = redissonClient. getCountDownLatch ( "door" ) ;
door. trySetCount ( 5 ) ;
door. await ( ) ;
return "关门了" ;
}
@ResponseBody
@GetMapping ( "/gogogo/{id}" )
public String gogogo ( @PathVariable ( "id" ) Long id) {
RCountDownLatch door = redissonClient. getCountDownLatch ( "door" ) ;
door. countDown ( ) ;
return id + "桌的人都走了。。。" ;
}
获取三级分类redisson分布式锁
@Autowired
private RedissonClient redissonClient;
public List < CategoryVo > getCategoryJsonFromWithRedissonLock ( ) {
String uuid = UUID . randomUUID ( ) . toString ( ) ;
RLock lock = redissonClient. getLock ( "CategoryJson-lock" ) ;
lock. lock ( ) ;
List < CategoryVo > dataFromDb = null ;
try {
dataFromDb = getDataFromDb ( ) ;
} finally {
lock. unlock ( ) ;
}
return dataFromDb;
}