package com.test.lockservice.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.test.lockservice.mapper.TicketMapper;
import com.test.lockservice.model.Ticket;
import com.test.lockservice.service.TicketService;
import com.test.lockservice.utils.RedisLock;
import org.redisson.RedissonMultiLock;
import org.redisson.RedissonRedLock;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Author sl
*/
@Service
public class TicketServiceImpl implements TicketService {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private TicketMapper ticketMapper;
@Autowired
private RedissonClient redissonClient;
@Override
public void userRedisson(){
// 获取锁
RLock lock = redissonClient.getLock("lock");
try {
// 加锁
lock.lock();
//获取redis中的票数
String ticket = redisTemplate.opsForValue().get("ticket");
if(ticket!= null && ticket.length() != 0){
// 扣减票数
Integer integer = Integer.valueOf(ticket);
if(integer >0){
redisTemplate.opsForValue().set("ticket",String.valueOf(--integer));
}
}
} finally {
// 解锁
lock.unlock();
}
}
@Override
public void useFairLock() {
RLock fairLock = redissonClient.getFairLock("fairLock");
// fairLock.lock();
// 10秒钟以后自动解锁
// 无需调用unlock方法手动解锁
fairLock.lock(10, TimeUnit.SECONDS);
System.out.println("加锁成功"+Thread.currentThread().getName());
// 尝试加锁,最多等待100秒,上锁以后10秒自动解锁
// boolean res = fairLock.tryLock(100, 10, TimeUnit.SECONDS);
// fairLock.unlock();
}
@Override
public void useMutiLock() {
RLock lock1 = redissonClient.getLock("lock1");
// RLock lock2 = redissonClient.getLock("lock2");
//联锁所有的锁都上锁成功才算成功
RedissonMultiLock redissonMultiLock = new RedissonMultiLock(lock1);
redissonMultiLock.lock();
System.out.println("业务内容");
redissonMultiLock.unlock();
}
@Override
public void useRedLock() {
RLock lock1 = redissonClient.getLock("lock1");
// RLock lock2 = redissonClient.getLock("lock2");
RedissonRedLock readLock = new RedissonRedLock(lock1);
// 红锁在大部分节点上加锁成功就算成功
readLock.lock();
System.out.println("业务内容");
readLock.unlock();
}
@Override
public void useReadWriteLock() {
/**
* 读-读 不阻塞 读-写 阻塞 写-写 阻塞
* RReadWriteLock实现了java.util.concurrent.locks.ReadWriteLock接口
*/
RReadWriteLock rwlock = redissonClient.getReadWriteLock("readWrite");
// 最常见的读锁
rwlock.readLock().lock();
// 写锁
rwlock.writeLock().lock();
// 10秒钟以后自动解锁无需调用unlock方法手动解锁
rwlock.readLock().lock(10, TimeUnit.SECONDS);
rwlock.writeLock().lock(10, TimeUnit.SECONDS);
// 尝试加锁,最多等待100秒,上锁以后10秒自动解锁
// boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);
rwlock.readLock().unlock();
rwlock.writeLock().unlock();
}
@Override
public void useSemaphore() {
/**
* RSemaphore 采用了与java.util.concurrent.semaphore相似的接口
* 资源限流信号量, 3个资源 6个线程, semaphore是单机版限流,RSemaphore是分布式限流
*/
RSemaphore semaphore = redissonClient.getSemaphore("semaphore");
try{
semaphore.acquire();
}catch(Exception e){
e.printStackTrace();
}finally {
semaphore.release();
}
}
@Override
public void useCountDownLatch() {
/**
* RCountDownLatch 采用了与java.util.concurrent.CountDownLatch 相似的接口和用法
* 一个线程 等待一组线程完事
* 班长等待所有同学走出门口在锁门 CountDownLatch是单机版 RCountDownLatch是分布式版
*/
RCountDownLatch latch = redissonClient.getCountDownLatch("anyCountDownLatch");
latch.trySetCount(6);
latch.countDown();
try{
latch.await();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void sellTicket(){
String uuid = UUID.randomUUID().toString();
// setnx 排他使用,如果获取锁不成功,则重试
while(!redisTemplate.opsForValue().setIfAbsent("lock", uuid,3, TimeUnit.SECONDS)){
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
//获取redis中的票数
String ticket = redisTemplate.opsForValue().get("ticket");
if(ticket!= null && ticket.length() != 0){
// 扣减票数
Integer integer = Integer.valueOf(ticket);
if(integer >0){
redisTemplate.opsForValue().set("ticket",String.valueOf(--integer));
}
}
} finally {
String script="if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";
this.redisTemplate.execute(new DefaultRedisScript<>(script,Boolean.class), Collections.singletonList("lock"),uuid);
}
}
@Override
public void checkAndLock(){
RedisLock lock = new RedisLock(redisTemplate, "lock");
lock.lock();
// 查询票数
Ticket ticket = ticketMapper.selectOne(new QueryWrapper<Ticket>().eq("sell_company", "12306"));
// 判断不为空和票数大于0
if(ticket!=null&& ticket.getCount() > 0){
ticket.setCount(ticket.getCount()-1);
ticketMapper.updateById(ticket);
}
// 测试可重入
testRepeatEntry();
lock.unlock();
}
public void testRepeatEntry(){
RedisLock lock = new RedisLock(redisTemplate, "lock");
lock.lock();
System.out.println("redis分布式锁测试可重入");
lock.unlock();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
lock-service.zip (14个子文件)
lock-service
pom.xml 2KB
src
test
java
com
test
lockservice
LockServiceApplicationTests.java 226B
main
resources
application.properties 389B
java
com
test
lockservice
mapper
TicketMapper.java 815B
LockServiceApplication.java 335B
controller
TicketController.java 724B
utils
RedisLock.java 3KB
service
TicketService.java 346B
impl
CyclicBarrierTest.java 961B
TicketServiceImpl.java 7KB
SemaphoreTest.java 921B
CountDownLatchTest.java 744B
model
Ticket.java 1KB
config
RedissonConfig.java 619B
共 14 条
- 1
资源评论
小趴菜不能喝
- 粉丝: 395
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功