gpt4 book ai didi

java - 在一个 @Singleton-bean 中并行运行不同的 @Schedule-methods

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:05 24 4
gpt4 key购买 nike

我有一个单例 bean,基本上如下所示:

@Singleton
public class Timer {

@Schedule(minute="*/3", hour="*")
public void method1() {
// Task may take 10 minutes sometimes
}

@Schedule(minute="*", hour="*")
public void method2() {
// This should run even though method1 is running
}
}

当 method1 运行时,我不希望再次调用 method1(并行)。这就是我使用单例 bean 的原因。这按预期工作。但我注意到,当 method1 运行时,method2 也没有被调用。我希望方法 2 能够运行,即使方法 1 运行了很长时间。

(这些方法正在调用不同单例bean中的方法)

最佳答案

您必须创建两个单例类,一个包含 method1,另一个包含 method2。当前发生的情况是,当客户端执行方法 1 时,单例的所有方法都被其他客户端锁定,因为这些方法的默认锁定类型是 LockType.WRITE。由于您不希望并行执行相同的方法,因此必须使用此锁类型,但这将锁定单例的所有方法以进行并发访问。 Oracle's documentation说:

If a method is of locking type WRITE, client access to all the singleton’s methods is blocked until the current client finishes its method call or an access timeout occurs

关于java - 在一个 @Singleton-bean 中并行运行不同的 @Schedule-methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22682342/

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