• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Mike London

Does my lock/Unlock approach make sense?

Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic