Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does my lock/Unlock approach make sense?

 
xiao gao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have three method private Set getlocks()
public void lock(int record) throws IOException
public void unlock(int record) to make record locking.
I also add two variable, locks which is set object and isDatabaseLocked which is a Boolean value.
I use a HashSet object called locks to keep the record numbers. If one thread calls the lock(int record) method, it first looks up the locks object to find out if there is the same record in the locks collection. If there is no the same record number in the locks object, this thread puts the record number into the locks object, then this thread will do some operation on this record in the database. After database operation, this thread calls unlock(int record) to remove the record number from the locks and calls getLocks().notifyall() to notify all the other threads waiting for modifying records.
If one thread finds the same record number in the locks, it called getLocks().wait() to wait until be notified by another thread which removes the record number from the locks object.
The reason I use HashSet to contain the record number is that HashSet only allows unique object and provide a good performance on internal searching. That is what I want for the locks object. Since I need a thread-safe locks object , I use getLocks() method to return a thread-safe locks object.
According to the requirement, while record number is �C1, the whole database will be locked. I implement this feature in lock(int record) method.
If the record is ��-1��, it makes isDatabaseLocked to true. Each time, one thread looks up the locks object, it first check isDatabaseLocked value, if it is ture, this thread called getLocks().wait() immediately. If isDatabaseLocked is false, this thread takes another check on locks object to find out if the same record number is in the locks object.

Any advice?
Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic