I am working on implementing a very specific health check strategy for my service. Here are the details :
All the application threads update a mutable Spring bean "problemDetectedTimestamp" when they come across a problem.
I have a background thread(implemented using ScheduledExecutorService) running every second which updates another mutable Spring bean "isServiceHealthy" based on the value of "problemDetectedTimestamp". To elaborate, the background thread checks whether the value of "problemDetectedTimestamp" falls in the last thread execution interval. If yes, the "isServiceHealthy" flag is updated to "false". I have some other application logic which is dependent on "isServiceHealthy" flag.
Now, we have multiple threads running and as such, I would want the "problemDetectedTimestamp" to be updated in a thread safe manner without incurring the overhead of using "synchronized" blocks. I am considering declaring "problemDetectedTimestamp" as a volatile variable to ensure that writes from application threads are atomic and my background thread reads it from memory rather than from its local cache. But, I am not sure of how to declare the bean as "volatile" in Spring XML as "volatile" is a concept specific to java/c++ and I am pretty sure Spring is not tighly coupled with java as such.