SCJP 1.4, SCJD
Note that the Hashtable is synchronized, but you are calling it within a synchronized block. Therefore you are nesting synchronized blocks (implicitly, not explicitly) - this is a bad thing.
Anton Golovin ([email protected]) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Originally posted by Anton Golovin:
The methods of the Data class, however, are synchronized - all of them. Alternatively, there could be synchronized blocks for HashMap within the lock/unlock methods, to limit the scope of synchronization.
SCJP 1.4, SCJD
SCJP 1.4, SCJD
SCJP 1.4, SCJD
Originally posted by Oliver Roell:
Why shall we not synchronize the complete methods lock and unlock? It's the safest way to avoid nested synchronizing, and every access to the raf-file and the lock-HashMap would be thread-safe. The only drawback is less performance, but it's better than risking a deadlock or inconsistent datas.
SCJP 1.4, SCJD
I think, I will wait until you have got your results. When it's a fail or a 44/80 locking score
SCJP 1.4, SCJD
Originally posted by mike acre:
Peter,
I don't think that is sufficient.
What you propose is fine during lock/unlock but what about the check during update/delete? They will read that map, and they may do so whilst another thread is writing to the map in a lock method. In this circumstance the map must be at least internally synchronised.
Originally posted by peter wooster:
My updateRecord and deleteRecord methods call a validateLock method in the LockManager that synchronizes on the Map. They then call the writeRaw method that synchronizes on the RandomAccessFile.
Of course, all accesses to the Map must be made from code that synchronizes on the Map. I use the smallest blocks of code possible.
SCJP 1.4, SCJD
Originally posted by mike acre:
But that isn't true. All access do not require to synchronise on the map.
If the Map is in a multithreaded environment and there are only non-structural changes to the map then there is no need to synchronise on the map and no need to even have a synchronised map. I realise that this is not the case.
Our typical case is that there are structural modifications to the map in lock/unlock methods and non-structural accesses in update/delete. Therefore the only synchronisation required in update/delete is with an internally sychronised map (ie Collections.synchronisedMap), this is the case because they do not modify the map but there is concurrent access that does.
This I believe is more minimal sync than you are proposing.
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|