(All of this is in a multi-threaded environment) I have a scenario in my code which requires me to update the static variables in a class. Now, based on the latest combination of the variables, the code might enter one flow or the other. I've used synchronize to allow only one thread to update the variables. Since, I also want any other thread to notice the most recent value only, I've declared the two variables as volatile too. I've been reading about the differences b/w the two keywords, and I believe that both of them have to be used to achieve what I want. The read accesses are not synchronized, only the part where I write to the variables is synch-ed.
I am new to the multithreading domain in Java, and would like to know if this is a good practice. Is there a better (more efficient) way of achieving this in Java?