Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on thread-safe singleton for URLYbird

 
oli mueller
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question regarding the URLYBird Data -class and the threadsafe Singleton problem...

I would like to make the data-class a singleton and it should have a RandomAccessFile-member, initialised to the path of the data file. Now, ideally I would not like to hardcode the path to that file, but I struggle to find a way to do this, because as far as i know, you have to initialise the static "instance"...see below:
class Data{
private static instance = new Data("Hard-coded-path-to data file...((...")
private Data(String dataFile){
this.filePointer = new RandomAccessFile(dataFile, "rw");
}

public static getInstance(String dataFile){
if(instance == null){
//code will never be called...
instance = new RandomAccessFile(dataFile);
}
return instance;
}
}

...obviously, i can initialise the static instance with the hard-coded path to the file, but i dont find that very sexy. Setting instance to null however would not be threadsafe, correct??? How about making the whole getInstance()-method synchronized??...How have you solved this problem??

THANKS ALOT!!!...
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Oli. Welcome to JavaRanch!

Now, ideally I would not like to hardcode the path to that file, but I struggle to find a way to do this, because as far as i know, you have to initialise the static "instance"...


Well champ, even if you wanted, you have to let the user provide the path to the .db file when the application is started. With this, you can instantiate your Data class properly.

Now, you mentioned a "static instance". I'll tell you, in my assignment, my Data class was also a singleton. But... it doesn't have to be. You can instantiate it only once, passing the path in the constructor, instantiate it only when the application is started and use it throughout the application. In order to see how you can do this, please take a look here, section 4.1 more specifically. This is a paper that I wrote some days ago, and I present there a way to implement the Data class in a thread-safe way without making it a singleton. I think it will be helpful!
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Perillo wrote:I'll tell you, in my assignment, my Data class was also a singleton. But... it doesn't have to be. You can instantiate it only once, passing the path in the constructor, instantiate it only when the application is started and use it throughout the application. In order to see how you can do this, please take a look here, section 4.1 more specifically. This is a paper that I wrote some days ago, and I present there a way to implement the Data class in a thread-safe way without making it a singleton. I think it will be helpful!


If I am reading this correct, then the Data class in your paper is thread-safe & is not a singleton. But in your assignment you made the Data class a singleton. Was the Data class in your assignment thread-safe also?
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:If I am reading this correct, then the Data class in your paper is thread-safe & is not a singleton. But in your assignment you made the Data class a singleton. Was the Data class in your assignment thread-safe also?


Yes, you are reading this correctly. I did this because I thought it was easier to say Data.getInstance() wherever I was and get the same object. But today, after thinking a lot about this, I think that a best approach is to not make it a singleton and instantiate the Data class only once. The Data class of my assignment was certainly thread-safe. But, if you instantiate the Data class only once and use the same object throughout the application, then the effect is pretty much the same.
 
oli mueller
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roberto,

thanks a lot for your answer...I have downloaded your paper and will read it tonight. THANKS....however, from my point-of-view making the Data class a singleton would ensure (at least within one JVM) that there is only one instance. With your approach (esp. when the class is public) you can only "hope" that clients will not be stupid and implement several instances....for this reason, I would still actually lean towards the singleton solution, but I guess this is exactly one of those design decisions and as long as sufficient arguments are given in the choices, I guess either option is ok.....

However, I think what is important is that IF i decide to go for the singleton solution, I should implement it the right way....and this is where i am stuck at the moment. Would you (and maybe others who have used the singleton) agree on the following statements???....

(1) Since the Data class must be parametrizable with the file-path, there is NO way to initialize the statice singleton-instance straightaway. Instead, it must be set to null and the usual getInstance()-method must be used, which will initialize it.

(2) In order to make getInstance() threadsafe it can be enhanced with synchronized (i mean, of course it can, but would you guys do it as well???....)

(3) Also, the getInstance() method should get a parameter for the filepath which is then passed on to the private constructor, when the singleton instance is initialised....

Thanks a lot - i am curious how you guys have done it...
THANKS:
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Oli!

How about you just create the Data class instance as a thread-safe singleton (with getInstance()) and then invoke setDataPath(...) on it?

If one would use some of the Data class methods without setting the data path, you could throw i.e. IllegalStateException to prevent such operations.

Cheers!

Edit: this is my approach: http://www.coderanch.com/t/513819/java-developer-SCJD/certification/Singleton-initialization-exceptions-thread-safety
 
Roel De Nijs
Sheriff
Posts: 9838
103
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I initialized my static singleton-instance straightaway and also used the approach suggested by Pedro Kowalski.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oli mueller wrote:With your approach (esp. when the class is public) you can only "hope" that clients will not be stupid and implement several instances....for this reason, I would still actually lean towards the singleton solution, but I guess this is exactly one of those design decisions and as long as sufficient arguments are given in the choices, I guess either option is ok.....


Well, I'm not sure if I like the idea of having to code in a certain way in order to not allow whoever is going to maintain the software to make things wrong... I think that we should deliver the easiest and cleanest possible solution. This and a well-written JavaDoc have to be the tools for the people who will maintain the software in the future. If they don't read carefully the code and don't read the JavaDoc, then it is pretty likely that they will mess everything up, no matter if your Data class is a singleton or not.

And by the way, just a curiosity, recently, there was an interview with the GoF because it's been 15 years since the book now. This was in the magazine I act as tech member here in Brazil. They were asked what they would change in the book if there was a second edition of it. Guess what was the pattern that they will drop from the book? Singleton!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic