当前位置:网站首页>Redis distributed lock + thread pool
Redis distributed lock + thread pool
2022-07-21 02:21:00 【Learning MicroStation】
@SpringBootTest
@Slf4j
public class TestDo {
@Resource
private RedisUtil redisUtil;
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
@Test
public void test() {
List<Long> idList = new LinkedList<>();
for (int i = 0; i < 10; i++) {
idList.add(Long.valueOf(i));
}
for (Long shopId : idList) {
if (null == shopId) {
continue;
}
String redisLock = "test:demo:" + shopId;
String value = redisUtil.get(redisLock, String.class);
log.info("value:{}", value);
if (Objects.equals(value, shopId.toString())) {
continue;
}
redisUtil.set(redisLock, shopId.toString());
log.info("shopId:{}", shopId);
scheduledExecutorService.execute(() -> {
try {
String threadName = Thread.currentThread().getName();
long threadId = Thread.currentThread().getId();
log.info(" The thread name is threadName:{}, Threads id by threadId:{}",threadName,threadId);
String startDate = formatter.format(new Date());
// Simulation business
try {
//5 second
log.info("start===");
Thread.sleep(5000);
} catch (InterruptedException e) {
log.error("e:{}", e);
}
String endDate = formatter.format(new Date());
} catch (Exception e) {
log.error("threadPool.execute:", e);
} finally {
// Release the lock
redisUtil.deleteCache(redisLock);
}
});
}
}
}
边栏推荐
- kettle
- KVM virtualization
- redis应用
- 36 - [go] io flow of golang
- Function of 1 * 1 convolution kernel
- Leetcode sword finger offer 11 Rotate the minimum number of the array
- (pc+wap) Zhimeng template accounting service website
- Batch梯度下降、随机梯度下降和mini-batch梯度下降
- Shengsi YiDianTong | deep learning analysis of classical convolutional neural network
- Regular expression tutorial notes
猜你喜欢
Share a fun JS game
精确率和召回率 与 置信度之间的关系
网络与VPC之动手实验
SQL Server 2008 R2 卸载失败
647. 回文子串【双指针:两种对称情况-回文串】(字符串S中回文子串的个数)
Protocol details of network principles
regular expression
Explain fcos: full revolutionary one stage object detection in detail
解决梯度爆炸和梯度消失
[special for training courses] Introduction to tee components
随机推荐
目标检测的进阶-one stage
Multifunctional to meet the needs of tourists, VR panorama promotes the ecological integration of smart scenic spots
RESNET knowledge points supplement
Int type changed to double
特殊类的设计(单例模式)
Animation function encapsulation (slow motion animation)
MySQL - partition column (horizontal partition vertical partition)
Project 1: Xiaomi official website
存储过程和for循环插入数据
Custom MVC add query
Pyhon crawls Beijing's 10-year weather data
【培训课程专用】Trustzone--TZC400设置安全内存
Ensemble learning of machine learning
【Pytorch】Tensorboard用法:标量曲线图、直方图、模型结构图
分享一个好玩的JS小游戏
自定义MVC增查
数据可视化之堆叠柱状图带你了解宠物数据
卷积神经网络CNN常用的几个模型
详解激活函数
06.02 二叉树遍历