This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
My question is, If I create a singleton class Data, either network mode or local mode, only client can have one instance of Data, so only one client can work with the data file at any given time.
Do I still need synchoniation in this case?
How does being a singleton limit the number of clients that can simultaneously use the Data class? Each client will have at least one thread at any time its accessing the server. Each of those threads can be using that Data instance, its quite possible for there to be several using it at once. If you use a RandomAccessFile you will need to seek and read to get data and you don't want another thread to move that data pointer between the seek and the read. If you cache records you will need to synchronize access to the cache.
The simple answer is yes, you will have multiple threads and you will need synchronization.
Joined: Jan 05, 2005
You are right. I was confused. With Singleton, there is only one instance in the JVM, but mutilple threads could still use the same instance, so I still need synchronization.
Joined: Jan 05, 2005
In my case, I use RandomAccessFile to access file. But in my Data class which implements all direct data actions such as update/read/write/unlock/lock.
I have different possible ideas about how to implement the sychronization.
1. In every action/method, there is one instance of RandomAccessFile. If I synchronize every method, I can only make sure one thread is going to use that particular method. Other threads might be able to use other methods. In another words, mutiple randomaccessFile could be pointing to the same file. This could lead to problem.
2. There is only one randomAccessFile instance in Data class, I will synchronize this RAF instance, so basically only one thread could use the RAF to access file at any given time. I don't have to synchronize any method at all.
I should do the same thing to the Lock hash map too.
In this case, I know for sure, the Hashmap lock is going to be OK, but I have doubt about RAF, in every method, I have to create a difference instance to point to the data file, you can't create the instance in the class level because of the FileNotFound exception.
Joined: Jun 13, 2004
All you need if you are using a singleton is to syncronize the methods that do the actual I/O, this keeps it simple. You could synchronize just the blocks that do the actual I/O on the RandomAccessFile member variable. You don't need a static RandomAccessFile variable since the singleton will guarantee a single copy of any member variables.
Joined: Dec 29, 2004
If each method has a different random acess file then just syncronizing the methods should be fine. I'm not sure what happens if two RAF's point to the same file though.
What I was talking about with the lock Object is to just create and Object not a RandomAccessFile at the class level. You just need something to sync with. If each method is using a seperate RAF, and there are no problems with two RAF's looking at the same file, then the lock Object is not needed you can just sync each method.