aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Do we need synchroniation with Singleton Data class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Do we need synchroniation with Singleton Data class " Watch "Do we need synchroniation with Singleton Data class " New topic
Author

Do we need synchroniation with Singleton Data class

Tommy Wan
Greenhorn

Joined: Jan 05, 2005
Posts: 14
I posted this question in this topic, and I got no response.
http://www.coderanch.com/t/186694/java-developer-SCJD/certification/does-Data-class-have-singleton


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?

Tommy
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Tommy Wan:
I posted this question in this topic, and I got no response.
http://www.coderanch.com/t/186694/java-developer-SCJD/certification/does-Data-class-have-singleton


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?

Tommy


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.
Tommy Wan
Greenhorn

Joined: Jan 05, 2005
Posts: 14
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.
Tommy Wan
Greenhorn

Joined: Jan 05, 2005
Posts: 14
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.

Could you comment on these?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
If you are going to use 'synchronized' You could create a class level Object to lock on:

something like that. If you can use Java 5 you may want to look at the new concurrent package. You should get better performance with that.
Tommy Wan
Greenhorn

Joined: Jan 05, 2005
Posts: 14
Hi Steve,

Basically you are telling me if I want to lock the RandomAccessFile object, I should create a globle RandomAccessFile object in Data class, and synchronize it.

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.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
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.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11508
    
  95

Hi Steven,

Originally posted by Steven Bell:
If you are going to use 'synchronized' You could create a class level Object to lock on:

something like that. If you can use Java 5 you may want to look at the new concurrent package. You should get better performance with that.


I think you made a typo - your lock object was an instance variable, not a class variable.

All objects implicitly have a static .class variable which should give you the desired result. That is, instead of using synchronized(lock) you could have used synchronized(Blah.class).

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
Consider Paul's rocket mass heater.
 
subject: Do we need synchroniation with Singleton Data class