• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do we need synchroniation with Singleton Data class

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I posted this question in this topic, and I got no response.
https://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
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tommy Wan:
I posted this question in this topic, and I got no response.
https://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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic