Search code examples
javajmxvolatile

Does jmx create a new thread for each invocation?


I have a class with a non-volatile field, and a mBean in the get method.

class Foo {
   int x;
   int get() {  return x; }
}

Can I be sure that the mBean will get the latest value of the variable? More generally, how jmx invokes the methods, does it create a new thread on each invocation? (in this case, it is assured that we get the latest value)


Solution

  • Yes, you would get latest value depending on x being read/written in a synchronized manner. If it is a primitive type, consider using AtomicInteger (or AtomicDouble etc.) to avoid writing synchronized constructs on your own.

    Whether JMX creates a new thread for each invocation could be simply found out by adding following line in your get() method:

    System.out.println("get() executed by thread: " + Thread.currentThread().getName());

    This would print thread name and if it varies in each invocation, that means differnet threads are used.