I am preparing to take SCJD with the help of Andrew Monkhouse (Great book!!! Eye opener for me to see a bigger picture in the development of an application. Thanks Andrew :-)).
I am also trying to implement DennyDVD Application with the .db file provided in the code samples, and trying to build the application with my knowledge.
I used 3 classes in total for Model's implementation.
1. DvdClient - implementation of the interface. Contains all the necessary methods exposed to user. Does nothing but delegating them to the next level.
2. DvdFileAccess - contains the implementation for translation of the DVD Commands to their physical implementation. All the file index-to-DVD and UPC-to-DVD mappings are available here. Despite Andrew's advice, I made it a singleton :-). Delegates the actual file writing to the next level.
3. DvdDataFile - contains the implementation for writing to the file. Uses RandomAccessFile to realize the .db file. All the methods to write to the file are static synchronized.
My logic of MVC Implementation would be many "DvdClient" models trying to use the other 2 clases in reading from and writing to the data file. Is this a right approach ? I mean, there would be as much Controllers and Views as the model is - all using the 2 classes in the back end. I tried the same implementation with RMI as well.
One major thing, I ve been a big bum in concurrency implementation and hitting my head all the time to find out how it works.
To start with, I synchronized the methods of DvdDataFile and used ReadLock and WriteLock for methods of DvdFileAccess.
Did a test with multiple controller threads (each with a model object) performing various operations on the file. Seems to be working fine.
But, am not convinced. Looks too simple, am missing something important here. Can anybody enlighten me here ?
I didn't go through thoroughly the DVD sample application. Because halfway through, I decided to use notifyAll() and wait() method. But, I guess it's not difficult to understand once you get hold of the concept of which object should be used to be synchronized. And of course beware of objects shared across threads.
SCJP6, SCJD, OCJWCD5
posted 10 years ago
Allan Cheong wrote:I didn't go through thoroughly the DVD sample application. Because halfway through, I decided to use notifyAll() and wait() method. But, I guess it's not difficult to understand once you get hold of the concept of which object should be used to be synchronized. And of course beware of objects shared across threads.
Thanks Allan :-)
I have used ReadLock (for getting and searching DVDs) and WriteLock (for adding, removing, modifying, renting and removing DVDs). Locks are similar to synchronizing on the object, I have learned and the DvdFileAccess is a singleton.
But, I have considered every function atomic (in the DvdFileAccess class). I am sure its gonna affect performance, rolling entire functions within lock. Do you have a suggestion for this ?
Also, I never had the track of the locks owned by each Object, with my assumption of locking 'whole' functions. How to overcome them ?
Performance wise if you are afraid of it will affect the application, you should write it down in your choices explaining why you choose to do it. However, I do not think performance will be an issue in this assignment. For instance in my assignment, my code will still run the lock and unlock method although in stand alone mode. It will be insignificantly slower but I chose to do it this way because of simpler design.
SCJP6, SCJD, OCJWCD5
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop