假设我有一个调度程序
@Component
public class Scheduler{
private static int counter = 0;
private synchronized void countIt(){
counter++;
}
@Scheduled(fixedDelay = 3000)
public void job1(){
countIt();
}
@Scheduled(fixedDelay = 6000)
public void job2(){
countIt();
}
}
不同的任务触发器在不同的情况下会调用countIt。
当两个或多个作业同时调用countIt时,就会导致饥饿。
谁能告诉我是否有办法避免这种情况?
最佳答案
这里没有死锁!
使用具有公平策略的 ReentrantLock。如果您不知道 ReentrantLock,请用 google 搜索。
private final ReentrantLock lock = new ReentrantLock(true);
关于java - Spring Scheduler同步方法饥饿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40042840/