简体   繁体   English

在Apache Tomcat应用程序中,MBean字段是否需要易失/同步才能使JMX更新出现在其他线程中?

[英]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 . 需要在其他线程中看到的任何更改需要标记为volatilesynchronized

// 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.

相关问题 为什么我需要对volatile上的多个线程使用同步? - Why do I need to use synchronized for multiple threads over volatile? JMX MBean - 在getter和setter上同步 - JMX MBean - synchronized on getters and setters Tomcat监视-connectionCount JMX mbean - Tomcat Monitoring - connectionCount JMX mbean 在 java 中同步以确保其他线程的内存更新? - Synchronized in java ensuring memory updates for other threads? 如何从tomcat网站访问JMX Mbean? - How to access a JMX Mbean from a tomcat website? 如果将对象分配给最终字段,其他线程是否会看到该对象的非最终/非易失性字段的先前更新? - If you assign an Object to a final field, will other threads see previous updates of that Object's non-final/non-volatile fields? JMX-应用程序部署中的MBean自动注册 - JMX - MBean automated registration on application deployment 在多个线程之间传递 POJO(无易失性字段,同步方法)并确保更改可见性的正确方法? - Correct way to pass a POJO (no volatile fields,synchronized methods) between multiple threads and assure visibility of changes? Tomcat JMX - 连接到服务器但无法找到我想要的MBean - Tomcat JMX - Connecting to server but cant find the MBean i want 使用JMX停止Apache Tomcat - Stop Apache Tomcat using JMX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM