This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Why so volatile

Nov 5th, 2012, 11:24 AM

I've read some of the source code, and wonder why you've used volatile in so many places.
I know it is a basic part of Java, and I can google what it does.
What I want to know is what were your (the spring integration developers) reasons for using?
What did you observe that indicated it was necessary?

The short answer is to ensure that in the multithreaded execution environment, the updates to the variable are visible immediately to all threads. In other words with various JVM optimization if current value of variable 'a' is let's say is set to 0 and Thread 1 changes it to 1, Thread 2 might still see it as 0. The 'volatile' keyword ensures that all writes to the variable go to main memory (not cached thread-locally).
But as I said, this only scratches the surface, there is more and reading the above would help.