Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

why do i care about locking when i use singleton fileaccess

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have a class that is singleton and its responsible for reading and writting to the database file. why do i still care about locking?
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

so there are more than one client will access this class and use the
methods provide ex
bookRecord :
1- read record to check if exist.
2- check if the record available for book
3- book the record.

if two clients use this method at the same time may be cause logical error
in the database. also the locking is business issue !.

best regards.
Mohamed darim
 
fei lin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i synchronize this method. its 1 atomic action.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fei, synchronizing the entire method, while technically could work, you need to take into consideration performance. Imagine a scenario where many threads are vying for access to the methods of your object. Each of them having to wait for the entire method to execute before they could possibly have their turn, would invariably result in poor performance. The best approach with multi-threaded programming is to synchronize only the resources that's state can be affected by multiple threads. This could be the RandomAccessFile object in your data class, or some sort of map object in a locking class. This approach does require you to think carefully about your design, but the improvements in performance are well worth it. Hope this helps.
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

thank Mike Ottinger to your reply, another reason that the DB interface is
general interface and does not provide contract that any clients must
synchronized prior to use.

Best regards.
Mohamed Darim.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic