简体   繁体   中英

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

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?

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? 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. Any changes there that need to be seen in other threads will need to be marked as volatile or 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. 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. In those cases, I do not mark the fields as volatile and it hasn't bitten us yet. YMMV.

FYI, the @JmxOperation annotation is from my SimpleJmx library .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM