1、本代码基于定时调度和异步执行同时处理,如果只加异步处理,会导致当前任务未执行完,下个任务到点也不会触发执行
@Scheduled(fixedRate = 50_000)
@Async
public void testThread() throws Exception{
ZkLock lock = new ZkLock(zkJob.getZK(), "/vman/analyse_job/lock2");
try {
if (!lock.acquire(20, TimeUnit.SECONDS)) {
log.warn("有其他线程进行分析任务的数据迁移,暂停本次执行");
return;
}
StringBuilder sb = new StringBuilder("处理成功的分析任务id:");
Integer startShareId = 1;
Integer endShareId = 100;
Integer count = 0;
while (startShareId.compareTo(endShareId) <= 0) {
try {
Thread.sleep(1000);
sb.append(startShareId).append(",");
} catch (Exception e) {
log.info("自助分析任务批量转换异常,jobId:{},exception:{}", startShareId, e);
}
startShareId++;
if (count++ % 20 == 0) {
log.info("当前分析任务id:{},转换成功的任务信息:{}", startShareId, sb.toString());
sb = new StringBuilder("处理成功的分析任务id:");
}
}
}catch(Exception e) {
log.info("定时任务,分析任务转换异常:{}",e);
}finally {
log.info("关闭分析任务的锁");
lock.release();
}
}
2、日志信息:
[2023-09-04 18:54:15.562] [INFO] [SimpleAsyncTaskExecutor-64] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:62,转换成功的任务信息:处理成功的分析任务id:42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,
[2023-09-04 18:54:23.082] [WARN] [SimpleAsyncTaskExecutor-65] [com.vip.vman.service.job.VmanConvertManager] >>> 有其他线程进行分析任务的数据迁移,暂停本次执行
[2023-09-04 18:54:23.083] [INFO] [SimpleAsyncTaskExecutor-65] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 18:54:35.629] [INFO] [SimpleAsyncTaskExecutor-64] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:82,转换成功的任务信息:处理成功的分析任务id:62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,
[2023-09-04 18:54:41.870] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 18:54:54.687] [INFO] [SimpleAsyncTaskExecutor-64] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 18:54:54.687] [INFO] [SimpleAsyncTaskExecutor-64] [com.vip.vman.client.zk.ZkLock] >>> zkLock release -> /vman/analyse_job/2023090418/lock2
[2023-09-04 18:54:55.852] [INFO] [SimpleAsyncTaskExecutor-66] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:2,转换成功的任务信息:处理成功的分析任务id:1,
[2023-09-04 18:55:15.934] [INFO] [SimpleAsyncTaskExecutor-66] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:22,转换成功的任务信息:处理成功的分析任务id:2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
[2023-09-04 18:55:36.024] [INFO] [SimpleAsyncTaskExecutor-66] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:42,转换成功的任务信息:处理成功的分析任务id:22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
[2023-09-04 18:55:41.866] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 18:55:56.091] [INFO] [SimpleAsyncTaskExecutor-66] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:62,转换成功的任务信息:处理成功的分析任务id:42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,
[2023-09-04 18:56:03.061] [WARN] [SimpleAsyncTaskExecutor-67] [com.vip.vman.service.job.VmanConvertManager] >>> 有其他线程进行分析任务的数据迁移,暂停本次执行
[2023-09-04 18:56:03.061] [INFO] [SimpleAsyncTaskExecutor-67] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 18:56:16.164] [INFO] [SimpleAsyncTaskExecutor-66] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:82,转换成功的任务信息:处理成功的分析任务id:62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,
[2023-09-04 18:56:35.223] [INFO] [SimpleAsyncTaskExecutor-66] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 18:56:35.223] [INFO] [SimpleAsyncTaskExecutor-66] [com.vip.vman.client.zk.ZkLock] >>> zkLock release -> /vman/analyse_job/2023090418/lock2
[2023-09-04 18:56:36.359] [INFO] [SimpleAsyncTaskExecutor-68] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:2,转换成功的任务信息:处理成功的分析任务id:1,
[2023-09-04 18:56:41.876] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 18:56:56.429] [INFO] [SimpleAsyncTaskExecutor-68] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:22,转换成功的任务信息:处理成功的分析任务id:2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
[2023-09-04 18:57:16.497] [INFO] [SimpleAsyncTaskExecutor-68] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:42,转换成功的任务信息:处理成功的分析任务id:22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
[2023-09-04 18:57:36.600] [INFO] [SimpleAsyncTaskExecutor-68] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:62,转换成功的任务信息:处理成功的分析任务id:42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,
[2023-09-04 18:57:41.895] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 18:57:43.098] [WARN] [SimpleAsyncTaskExecutor-69] [com.vip.vman.service.job.VmanConvertManager] >>> 有其他线程进行分析任务的数据迁移,暂停本次执行
[2023-09-04 18:57:43.099] [INFO] [SimpleAsyncTaskExecutor-69] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 18:57:56.675] [INFO] [SimpleAsyncTaskExecutor-68] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:82,转换成功的任务信息:处理成功的分析任务id:62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,
[2023-09-04 18:58:15.756] [INFO] [SimpleAsyncTaskExecutor-68] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 18:58:15.757] [INFO] [SimpleAsyncTaskExecutor-68] [com.vip.vman.client.zk.ZkLock] >>> zkLock release -> /vman/analyse_job/2023090418/lock2
[2023-09-04 18:58:16.890] [INFO] [SimpleAsyncTaskExecutor-70] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:2,转换成功的任务信息:处理成功的分析任务id:1,
[2023-09-04 18:58:36.962] [INFO] [SimpleAsyncTaskExecutor-70] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:22,转换成功的任务信息:处理成功的分析任务id:2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
[2023-09-04 18:58:41.901] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 18:58:57.032] [INFO] [SimpleAsyncTaskExecutor-70] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:42,转换成功的任务信息:处理成功的分析任务id:22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
[2023-09-04 18:59:17.101] [INFO] [SimpleAsyncTaskExecutor-70] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:62,转换成功的任务信息:处理成功的分析任务id:42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,
[2023-09-04 18:59:23.117] [WARN] [SimpleAsyncTaskExecutor-71] [com.vip.vman.service.job.VmanConvertManager] >>> 有其他线程进行分析任务的数据迁移,暂停本次执行
[2023-09-04 18:59:23.117] [INFO] [SimpleAsyncTaskExecutor-71] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 18:59:37.173] [INFO] [SimpleAsyncTaskExecutor-70] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:82,转换成功的任务信息:处理成功的分析任务id:62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,
[2023-09-04 18:59:41.897] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
3、使用 jstack 88023 |grep SimpleAsyncTaskExecutor-62 命令可以查看相关线程是否存在,可以看到,相关线程任务执行完成后,会自动消失
4、如果去掉异步,代码如下文章来源:https://www.toymoban.com/news/detail-700638.html
//@Async
@Scheduled(fixedRate = 30_000)
public void testThread() throws Exception{
ZkLock lock = new ZkLock(zkJob.getZK(), "/vman/analyse_job/lock2");
try {
if (!lock.acquire(20, TimeUnit.SECONDS)) {
log.warn("有其他线程进行分析任务的数据迁移,暂停本次执行");
return;
}
StringBuilder sb = new StringBuilder("处理成功的分析任务id:");
Integer startShareId = 1;
Integer endShareId = 60;
Integer count = 0;
while (startShareId.compareTo(endShareId) <= 0) {
try {
Thread.sleep(1000);
sb.append(startShareId).append(",");
} catch (Exception e) {
log.info("自助分析任务批量转换异常,jobId:{},exception:{}", startShareId, e);
}
startShareId++;
if (count++ % 20 == 0) {
log.info("当前分析任务id:{},转换成功的任务信息:{}", startShareId, sb.toString());
sb = new StringBuilder("处理成功的分析任务id:");
}
}
}catch(Exception e) {
log.info("定时任务,分析任务转换异常:{}",e);
}finally {
log.info("关闭分析任务的锁");
lock.release();
}
}
发现任务执行的线程只有一个 叫 scheduling-1文章来源地址https://www.toymoban.com/news/detail-700638.html
[2023-09-04 19:52:57.765] [WARN] [scheduling-1] [org.apache.curator.utils.ZKPaths] >>> The version of ZooKeeper being used doesn't support Container nodes. CreateMode.PERSISTENT will be used instead.
[2023-09-04 19:52:59.394] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:2,转换成功的任务信息:处理成功的分析任务id:1,
[2023-09-04 19:53:19.465] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:22,转换成功的任务信息:处理成功的分析任务id:2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
[2023-09-04 19:53:39.533] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:42,转换成功的任务信息:处理成功的分析任务id:22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
[2023-09-04 19:53:41.406] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 19:53:58.616] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 19:53:58.620] [INFO] [scheduling-1] [com.vip.vman.client.zk.ZkLock] >>> zkLock release -> /vman/analyse_job/2023090419/lock2
[2023-09-04 19:53:59.880] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:2,转换成功的任务信息:处理成功的分析任务id:1,
[2023-09-04 19:54:19.959] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:22,转换成功的任务信息:处理成功的分析任务id:2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
[2023-09-04 19:54:40.043] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:42,转换成功的任务信息:处理成功的分析任务id:22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
[2023-09-04 19:54:41.405] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 19:54:59.158] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 19:54:59.160] [INFO] [scheduling-1] [com.vip.vman.client.zk.ZkLock] >>> zkLock release -> /vman/analyse_job/2023090419/lock2
[2023-09-04 19:54:59.384] [WARN] [Thread-51] [org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2] >>> Lease already released
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /vma.vip.vip.com/vman/analyse_job/2023090419/lock2/leases/_c_2f05745b-600d-4d3c-aa61-eb518072383a-lease-0000000001
at org.apache.zookeeper.KeeperException.create(KeeperException.java:111)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.delete(ZooKeeper.java:873)
at org.apache.curator.framework.imps.DeleteBuilderImpl$5.call(DeleteBuilderImpl.java:250)
at org.apache.curator.framework.imps.DeleteBuilderImpl$5.call(DeleteBuilderImpl.java:244)
at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:109)
at org.apache.curator.framework.imps.DeleteBuilderImpl.pathInForeground(DeleteBuilderImpl.java:241)
at org.apache.curator.framework.imps.DeleteBuilderImpl.forPath(DeleteBuilderImpl.java:225)
at org.apache.curator.framework.imps.DeleteBuilderImpl.forPath(DeleteBuilderImpl.java:35)
at org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2$3.close(InterProcessSemaphoreV2.java:437)
at org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex.release(InterProcessSemaphoreMutex.java:67)
at com.vip.vman.client.zk.ZkLock$1.run(ZkLock.java:59)
at java.lang.Thread.run(Thread.java:750)
[2023-09-04 19:54:59.426] [ERROR] [Thread-51] [com.vip.vman.client.zk.ZK] >>>
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /vma.vip.vip.com/vman/analyse_job/2023090419/lock2/leases
at org.apache.zookeeper.KeeperException.create(KeeperException.java:111)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.getChildren(ZooKeeper.java:1650)
at org.apache.curator.framework.imps.GetChildrenBuilderImpl$3.call(GetChildrenBuilderImpl.java:230)
at org.apache.curator.framework.imps.GetChildrenBuilderImpl$3.call(GetChildrenBuilderImpl.java:219)
at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:109)
at org.apache.curator.framework.imps.GetChildrenBuilderImpl.pathInForeground(GetChildrenBuilderImpl.java:216)
at org.apache.curator.framework.imps.GetChildrenBuilderImpl.forPath(GetChildrenBuilderImpl.java:207)
at org.apache.curator.framework.imps.GetChildrenBuilderImpl.forPath(GetChildrenBuilderImpl.java:40)
at com.vip.vman.client.zk.ZK.getChildrenNum(ZK.java:408)
at com.vip.vman.client.zk.ZkLock$1.run(ZkLock.java:61)
at java.lang.Thread.run(Thread.java:750)
[2023-09-04 19:54:59.431] [ERROR] [Thread-51] [com.vip.vman.client.zk.ZkLock] >>>
java.lang.NullPointerException
at com.vip.vman.client.zk.ZkLock$1.run(ZkLock.java:61)
at java.lang.Thread.run(Thread.java:750)
[2023-09-04 19:55:01.197] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:2,转换成功的任务信息:处理成功的分析任务id:1,
[2023-09-04 19:55:21.280] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:22,转换成功的任务信息:处理成功的分析任务id:2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
[2023-09-04 19:55:41.354] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:42,转换成功的任务信息:处理成功的分析任务id:22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
[2023-09-04 19:55:41.400] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 19:56:00.429] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 关闭分析任务的锁
[2023-09-04 19:56:00.430] [INFO] [scheduling-1] [com.vip.vman.client.zk.ZkLock] >>> zkLock release -> /vman/analyse_job/2023090419/lock2
[2023-09-04 19:56:01.644] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:2,转换成功的任务信息:处理成功的分析任务id:1,
[2023-09-04 19:56:21.693] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:22,转换成功的任务信息:处理成功的分析任务id:2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
[2023-09-04 19:56:41.403] [INFO] [client-forward-message-scheduler] [com.vip.daas.client.DaasSession$DaasClientEndpoint] >>> Send heart beat message to master [10.199.210.210:9002]
[2023-09-04 19:56:41.774] [INFO] [scheduling-1] [com.vip.vman.service.job.VmanConvertManager] >>> 当前分析任务id:42,转换成功的任务信息:处理成功的分析任务id:22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
到了这里,关于基于springboot实现多线程抢锁的demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!