Hi all.
I have a web application that reads and writes from a text file. The different servlet request threads may need to read or write to/from this file, and so I want the read/writes to happen one at a time (synchronized that is), of course. Performance is not an issue in my particular case. It will only be used by two or three people at a time.
So, at first I thought I create a singleton class with synchronized NON-static read and write methods. The singleton would have a NON-static File member object that it uses for access to the file. A worker thread would call...
MySingleton instance = MySingleton.getInstance();
instance.read();
instance.write();
...and so forth. It seems that should work.
But then I thought, why not just make a purely static class? By that I mean that all the members functions (read() and write()) would be static and synchronized, AND the File member object would ALSO be static. It seems to me that accomplishes the very same thing without the overhead of writing a getInstance() method and making sure that only one instance is ever created of my object.
So, unless I am missing something (and I usually am), in this case functionally there is no difference in the two approaches and the "purely static" approach is less code and (who knows) maybe less memory consumption?
Can anyone tell me if I'm wrong? Should I be using a singleton instead?
Thanks!
John