Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should I synchronize on data in bookHotel method?

 
Sean Gildea
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I need to synchronize on data in my client GUIController method BookHotel?

I already synchronize on my low level class , FileIO which Data implements

 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't synchronize at that level, the question to ask yourself is "what resource am I protecting from multiple accesses?". In my implementation only a single thread accesses a Data instance at a time. That thread is the thread that is performing actions for the client over the network.

All the syncronization is in the lower level objects that represents the database file and the lock manager.

I syncronize on the RandomAccessFile instance to prevent problems where one thread moves the file pointer and another moves it elsewhere before it can read or write the data.

I also synchronize on the Map of locked resources and the Lock object that is waiting or being notified.

All this is carefully done to ensure that the order is always Map->Lock or Map->RandomAccessFile, never the other way around, so there is no danger of deadlock.
 
Sean Gildea
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks peter!

That answers my question!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic