I have to say every day I learn something new here, I hope to be good enough in
Java to contribute more someday!
I have a question about what would be the best way to solve a concurrency problem I'm having. Here's the problem statement as concise as I can make it:
The FrobNozz application stores WhizzBang data into a singleton Collection from a data feed. Many methods read from the WhizzBang Collection. Now and then the Collection needs to be refreshed with new WhizzBang data. What's the best way to deal with processes using the methods?
The structure in Pseudocode:
Just an example, whizzData's not always a List, it could be some other Collection (Hashmap, Queue, etc). When new data is available, "whizzData" is set to null, causing it to pull in new data. Checking for 'null' twice (as I understand it) checks this condition just in case a bunch of threads hit the initial 'null' check at once.
My problem is that there are a huge amount of functions that slice and dice the whizzData singleton (all strictly read operations, which simplifies it a bit). Obviously for the interests of speed, I don't synchronize any of these methods, but if 'whizzData' is updated, the threads in the slice-and-dice functions just go bonkers, as 'whizzData" changes in the middle of processing. Stacktraces everywhere. Not a pretty sight. "whizData" isn't updated very often, maybe two or three times a day at the very most.
So is there a way to say "everybody out of the pool!", let all the threads finish executing the slice-and-dice methods, but prevent new threads from entering the methods. Once all the threads have exited the methods, I could update "whizzBang", then blow the whistle and let all the threads go back to using the methods?
I am sure I am probably approaching the problem through brute force, and there is a simpler and more elegant way to do this kind of update. How would you do it?
Thanks a bunch!
-Derrick