This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question on thread-safe singleton for URLYbird Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question on thread-safe singleton for URLYbird" Watch "Question on thread-safe singleton for URLYbird" New topic
Author

Question on thread-safe singleton for URLYbird

oli mueller
Ranch Hand

Joined: Feb 13, 2011
Posts: 42
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

Joined: Dec 28, 2007
Posts: 2258
    
    3

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!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

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?


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

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

Joined: Feb 13, 2011
Posts: 42
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

Joined: Jul 13, 2010
Posts: 610

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


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

I initialized my static singleton-instance straightaway and also used the approach suggested by Pedro Kowalski.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on thread-safe singleton for URLYbird
 
Similar Threads
URLybird
URLyBird 1.3.1 DB Access layer design, pls comment!
NX: UrlyBird data file format question
NX: URLYBird / my approach of the reading problem
Using Properties File to set database path