gpt4 book ai didi

java - 如何避免在 Quartz 中同时运行两个作业?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:45:40 28 4
gpt4 key购买 nike

我在下面的代码中运行了两个作业。第一个间隔为 10 秒,另一个间隔为 3 秒。但最终在某个时候它们会同时执行。有什么机制可以避免这种情况

    JobDetail jDetail = new JobDetail("Job1", "group1", MyJob.class);
CronTrigger crTrigger = new CronTrigger("cronTrigger", "group1", "0/10 * * * * ?");
sche.scheduleJob(jDetail, crTrigger);

jDetail = new JobDetail("Job2","group2",MyJob2.class);
crTrigger = new CronTrigger("cronTrigger2","group2","0/3 * * * * ?");
sche.scheduleJob(jDetail, crTrigger);

最佳答案

没有完全回答你的问题,但这是你如何查询以线程安全的方式运行的东西:

//sched is your org.quartz.Scheduler
synchronized (sched) {
JobDetail existingJobDetail = sched.getJobDetail(jobName, jobGroup);
if (existingJobDetail != null) {
List<JobExecutionContext> currentlyExecutingJobs = (List<JobExecutionContext>) sched.getCurrentlyExecutingJobs();
for (JobExecutionContext jec : currentlyExecutingJobs) {

if (existingJobDetail.equals(jec.getJobDetail())) {
// This job is currently executing
}
}
}

关于java - 如何避免在 Quartz 中同时运行两个作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8473863/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com