aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Should I synchronize on data in bookHotel method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Should I synchronize on data in bookHotel method?" Watch "Should I synchronize on data in bookHotel method?" New topic
Author

Should I synchronize on data in bookHotel method?

Sean Gildea
Ranch Hand

Joined: Jul 01, 2004
Posts: 81
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



SCJD, SCBCD, SCJP
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
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

Joined: Jul 01, 2004
Posts: 81
Thanks peter!

That answers my question!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should I synchronize on data in bookHotel method?