|
"I'm not back." - Bill Harding, Twister
I advise making separate classes for both the cache and the record
If the key to your cache is just an integer recNo, why use a HashMap? An ArrayList is faster and simpler, using recNo a the index of the List.
So I don't bother with partial cahing
Defensive copying of any objects passed as parameters is a good idea
Personally, I keep all records in memory, and I use an ArrayList (index is recNo) rather than a HashMap. To find(), I just loop through the ArrayList, synchonizing each individual access to the List; then after I've retrieved an individual record (and am no longer synced on the List) I sync on that record while I look at it. So there's no need for one long sync of the list; just lots of little ones.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
I commented a block in the first class, in the find method, which can bring a problem while multiple threads access it. Could anyone comment it?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
[Andrew] I think ThreadsafeDatabase1 has a few too many of these types of holes.
[Jim] ... after I've retrieved an individual record (and am no longer synced on the List) I sync on that record while I look at it.
I assume that in this case that no thread can aquire a write lock as long as there are any outstanding read locks. I think that this could potentially cause starvation in the threads trying to write.
Now if this were modified slightly so that the read or write locks were granted on a particular record number...
I don't really understand why lock() does not need synchronization, but unlock() does.
I assume that in this case that no thread can aquire a write lock as long as there are any outstanding read locks. I think that this could potentially cause starvation in the threads trying to write. You might have to write a queueing mechanism in ReadWriteLock to ensure that starvation does not occur.
ThreadSafeDatabase2/4:
Ok,
I think Andrew is right: Synchronizing on this is even simplier (but it will work only if Data class is a singletone or mutition).
Andrew, does it bring a lot not synchronizing find method?
Find is not synchronized (so such operation like isMatching() can run concurrently, but find invokes read for each record, which is synchronized.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
I am curious: why do you have three paramaters to your LockManager.lockRecord() method?
I assume that your create() method does not try to reclaim space left by deleted records? If it did - then do you have a potential for a user to request a lock on a deleted record (thereby creating the entry in the lock manager) after which a new record is created in that record number which can never be locked?
The simplicity offered does come at a price: the synchronizations are heavy operations, and we are jumping in and out of the synchronized blocks a large number of times for the find. But the advantage is that other operations will not be blocked until after the find is complete.
P.S. Phil, I also wonder why do you have so many arguments in a lock method:
(connectObj, this)?
The one we use (RWLock) is very elegant and it allows concurrent read.
I wanted to use it because of main reason: it should allow to decouple Data class and its synchronization class, so that IO deveoper should care about synchronization at all. It was the biggest advantage to other design.
As we just have found the bug in lock method, it doesn't allow us anymore to decouple synchronitazion and Data Access class.
The biggest advantage of design from Andrew is simplicity, disadvantage: it doesn't allow concurrent read. Andrew has very painfull (for me) argument: simplicity is an issue, not a performance.
I just wanted to know all your thought and how you are going top justify readwritelock synchronization strategy against others. How are you going to justify that it is good idea to have in Data class directly?
You can't imagine how wonderful it is to see at least somebody advocating the same design as I have !
Now I am less convinced by your approach of handling InterruptedException. It is not an error of any kind IMO, just a signal that interrupt() method has been called on the thread.
And interrupt() is not a deprecated Thread method (like stop() and supend() are), even if some book (but I didn't read it) discourage you to use it for any good or bad reason.
I really don't know. My only purpose was to be "transparent" to the interrupt() and isInterrupted() methods. By throwing some IOInterruptedException, you force the caller to catch it (and BTW it has nothing to do with some IO exceptional condition), and you let the interrupted status cleared.
My only purpose was to be "transparent" to the interrupt() and isInterrupted() methods. By throwing some IOInterruptedException, you force the caller to catch it.
Originally posted by Philippe Maquet:
Hi Vlad,
Yes I can imagine as I feel the same thing. You're right, let us congratulate us while we are just the two of us talking to each other : Max, Andrew or Mark will come shortly and ...
Phil.
[Philippe] Well, I disagree here with Andrew (it's so rare ! )
Yes I can imagine as I feel the same thing. You're right, let us congratulate us while we are just the two of us talking to each other : Max, Andrew or Mark will come shortly and ...
[Philippe] my MultiReadSingleWriteSynchronizer class in 200 lines long : not a big deal !
[Max]: I've been deliberately keeping out of this thread
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
"I'm not back." - Bill Harding, Twister
Note, that reserveDVD, releaseDVD is something like lock/unlock, not beforeRaed, afterRead, but the idea is the same. As I have asked him what happens if modifyDVD fails (exception will be thrown, but DVD will not be released) he says nobody cares, it is enough to become J2CD...
[Philippe]: If I was such a grader and if I had no other instructions, I would probably do the job as follows ... choose parts of the source code and read it, parts which interest me, parts for which I may suspect issues from the choices.txt content and finally parts I choose randomly.
Does anybody (Max for sure) know how graders work ?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
(Given how rapidly my assignment got marked, I am sure that the examiner did not inspect very much of my code - they probably just picked specific sections.)
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|