“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Can I ask why it's necessary to use a HashMap that records the current Thread ? Why do we need the thread recorded ? Is there any benefit to this over using a HashSet and recording the record number as an Integer() ?
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Actually, you need it in both. Because the HashMap locks is static, every instance of the Data class shares the same one. Any thread attempting to enter a synchronized block on this object will go into a waiting state if another thread has the exclusive lock on it. Feel free to correct me if I am wrong in this understanding. I am basing much of this from the Threading chapter in Max's book.
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Outstanding! I was wondering if a new book was going to be released with the advent of Java 5.0! Granted, I will have my SCJD by then (unless something goes horribly, horribly wrong), but I will still pick up a copy for reference if it is as good as the last one!Just FYI, an example (from some book due out in a few months)
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Originally posted by Andrew Monkhouse:
I strongly recommend against "swallowing exceptions" as you have done (just ignoring an exception). The only time I personally think this is acceptable is when the exception is expected. In all other cases you should either log it, or re-throw it, or both.
Regards, Andrew
42
Originally posted by Matt Garner:
I don't mean to hijack this thread, but I've used an update check while an object is waiting for the lock. The update check will throw an update exception once the object has acquired the lock, if the record has been deleted, book or even modified while waiting for the lock.
Does this update check seem like overkill too much?
SCJP 1.4, SCJD
Originally posted by Kris Reid:
...
peter
I did use RMI not sockets. It sounds like that is a problem?
wont the Thread that locks, updates, unlocks the record be the same one still?
...
SCJD, SCBCD, SCJP
Originally posted by Sean Gildea:
Kris,
I got an 80/80 on my locking and I believe the main reason was because I chose to check that the record was not deleted before updating. I basically did 2 checks on the record before updating, the first before the wait and the second after it was released from waiting.
[ March 28, 2005: Message edited by: Sean Gildea ]
Originally posted by Kris Reid:
My Uni Lecturer pointed out that in my unlock method I have
if (some condition)
unlock(record number)
it should be a while loop as you as you can't always unlock a reocrd
i.e. something might throw an exception so it should try again