Two Laptop Bag*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes DataFileAccess in Andrews book - needs synched Constructor? 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 "DataFileAccess in Andrews book - needs synched Constructor?" Watch "DataFileAccess in Andrews book - needs synched Constructor?" New topic
Author

DataFileAccess in Andrews book - needs synched Constructor?

Alan Mc Kernan
Ranch Hand

Joined: Oct 13, 2005
Posts: 59
Does the constructor need to be synch'd on DataFileAccess.class ?
If two creates come along at the same time, is'nt there a chance that both of them could step into the create new RandomAccessFile logic?

i.e Thread 1 see's database = null and steps into if block
Thread 1 gets suspended before it creates the RAF
Thread2 comes along and sees that database = null, steps into if block

Can this occur, or am I missing something?
Ed Tse
Ranch Hand

Joined: Sep 18, 2003
Posts: 183
Yeah, I saw that too. Other classes that implements singleton would do synchronization on the constructor.


SCJP, Pre-SCJD (URLyBird 1.3.1), Teradata Cert'd Prof
Alan Mc Kernan
Ranch Hand

Joined: Oct 13, 2005
Posts: 59
So should this be synch'ed somehow then ?
Gareth Llewellyn
Greenhorn

Joined: Nov 16, 2005
Posts: 11
The locking mechanism is wrapped around each critical part of DvdFileAccess, which I guess is similar if not the same as DataFileAccess.

private static ReadWriteLock recordNumbersLock
= new ReentrantReadWriteLock();

Its thread safe. The class doesn't need to be a singleton (LockManager and LogManager style classes are another matter...)

You end up making the whole class thread safe anyway by implementing a lock manager / mechanism on the Data class that uses DvdFileAccess / DataFileAccess.

Personally I didnt use the locking mechanism he employed, but a threadsafe record number / file location map instead:

private Map recordNumberFileLocationMap = Collections.synchronizedMap(new HashMap());
Leo Ho
Ranch Hand

Joined: Mar 31, 2005
Posts: 36
Besides, I'm not sure that the call to this line "database = new DvdFileAccess(dbPath);" in the construction of DvdDatabase whenever a new client connects to the server is safe since "database" is a static field. I would feel more comfortable if the "database" is somehow created once and never gets re-assigned.

How other people think?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well, I am just preparing to start writing the code for my assignment. And I am bit confused with this constructor synchronization.

I guess you must be talking about some constructor modifying or accessing resources possibly used by another threads.

Because it would not make sense to synchronize a constructor since until the object is not completely constructed it is not subject to synchronization.

However it is possible to have a synchronization block within an constructor, as long as the lock is not based on the object itself (this).

Now, with the new locking mechanisms present in Tiger, I just wonder if all this constructor synchronization you're talking about might be a mistake.

I think it may be if it is based on the suposition that the constructor itself should be synchronized to avoid another threads to modify the state of the object while it is been constructed, because no other threads will have access to the object until it is actually constructed.

However if you refer to the posibility that another thread modifies, for instance, an static member variable, while the constructor is using it, than I thik it may be alright.

Regards,
Edwin Dalorzo.
[ April 09, 2006: Message edited by: Edwin Dalorzo ]
Anna Hays
Ranch Hand

Joined: Nov 09, 2003
Posts: 131
Hi,

I haven't thought about this before for a singleton class. This is how I implement it.


Do you think synchronizing the getInstance() is good? I think it will prevent when 2 threads are calling that method and get to the line before the instance is assigned to a Data instance. But I haven't seen synchronization on the getInstance() anywhere in a singleton class. I wonder why.
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Hi Anna -

For your class to follow the singleton pattern properly as per your example, you should make the constructor private. Unless I'm much mistaken, as you've written it, there's nothing stopping me coming along and doing:



And I've broken the singleton invariant.


Do you think synchronizing the getInstance() is good?


Yes - I think it's necessary. Otherwise you could get a race situation in which you end up with multiple instances again.

If you want a good reference, try Effective Java by Joshua Bloch - it has a great many useful tips.
Anna Hays
Ranch Hand

Joined: Nov 09, 2003
Posts: 131
Thanks, I will keep the synchronization on the getInstance().

I do have a private construtor in my code, forgot to cut and paste here.

I get a feeling I can always do it better and don't know when I am going to submit my assignment.
 
GeeCON Prague 2014
 
subject: DataFileAccess in Andrews book - needs synched Constructor?