I like simplicity so this seems a nice solution to me
I oftend find myself agree with Eugene, but not in this case.
The code in you locking code isn't synchronized on anything, so it's not atomic(even if it's a single line of code). Say both clients A and B want to lock record #2. Client A checks to see if record @2 is locked. It currently isn't, so A tries to lock record # 2. But A get switched out(because it's thread got switched out). Now client B could check to see if record #2 is locked: it isn't, because A's not done. So B actually succeed in locking record #2. B finishes, and A wakes up again Now, because A has no way of knowing that record #2 has been locked while A's thread was napping(remember, you're past the if statement now), A goes ahead and locks record #2, effectively hijacking it from B. See the problem?
Originally posted by Salish Tankard:
Would it work if I synchronize the record number object (a Vector or a SortedSet)? By doing this, there is only one client can access this object at a time. What about two clients approach the object at the SAME point (rare situation, but possible?), who will get the object? How should I take care of this situation? Thank you
I would suggest a simpler approach: one static ArrayList/Hashmap object in the Data class.
Originally posted by Eugene Kononov:
In my opinion, this will make the responsibilities of Data class too broad. The purpose of the Data is to describe the database and provide the interface to manipulate the data.