[英]In an Apache Tomcat application, do MBean fields need to be volatile/synchronized for JMX updates to appear in other threads?
I've been exposing beans in our Spring web applications in case we need to make configuration changes on the fly. 我一直在我们的Spring Web应用程序中公开bean,以防我们需要动态地进行配置更改。 Recently I've been reviewing concurrency and I started to wonder what happens in other threads when you mutate one of these beans through JMX?
最近我一直在审查并发性,我开始想知道当你通过JMX改变其中一个bean时,其他线程会发生什么?
Does JMX have some way of forcing a memory model refresh so you don't need to worry about making the field volatile/synchronized to ensure other threads see the change? JMX是否有某种方法可以强制刷新内存模型,因此您不必担心使字段易失/同步以确保其他线程看到更改?
When Tomcat creates a new thread to handle a request, that thread will see the changes even if the field is not thread-safe, correct? 当Tomcat创建一个新线程来处理请求时,即使该字段不是线程安全的,该线程也会看到更改,对吗? So unless I need the change to immediately take effect in current request threads is there any reason to worry about concurrency issues?
因此,除非我需要更改以立即在当前请求线程中生效,否则有任何理由担心并发问题?
The JMX handler is not a special thread. JMX处理程序不是特殊线程。 Any changes there that need to be seen in other threads will need to be marked as
volatile
or synchronized
. 需要在其他线程中看到的任何更改都需要标记为
volatile
或synchronized
。
// this needs to be volatile because another thread is accessing it
private volatile boolean shutdown;
...
@JmxOperation(description = "Shutdown the server")
public void shutdownSystem() {
// this is set by the JMX connection thread
shutdown = true;
}
That said, typically JMX values are statistics or configuration settings that I don't mind being lazily updated when the next memory barrier is crossed. 也就是说,通常JMX值是统计数据或配置设置,我不介意在下一个内存屏障被越过时懒惰地更新。 This applies to counters and other debug information as well as booleans and other values that are only set by the JMX although they are used by other threads.
这适用于计数器和其他调试信息以及布尔值和仅由JMX设置的其他值,尽管它们由其他线程使用。 In those cases, I do not mark the fields as
volatile
and it hasn't bitten us yet. 在这些情况下,我没有将字段标记为
volatile
,并且还没有咬过我们。 YMMV. 因人而异。
FYI, the @JmxOperation
annotation is from my SimpleJmx library . 仅供参考,
@JmxOperation
注释来自我的SimpleJmx库 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.