• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do I need to synchronize read()? (B&S)

 
Gang Chen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks:

I don't know if I need to synchronize read() method in Data.java.
The specs I got says "You may assume that at any moment, at most one program is accessing the database file" I am sure I need to synchronize update(), delete(), lock() and unlock() since those methods modify the db file. However, does that "access" also include reading the db file? If so, the performance will take a hit.

Can anybody clarify it please?

Thanks a lot!
 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gang - I would take that line to mean that you may assume that your program is the only one accessing the database file. In other words you don't need to worry about weird effects arising from the legacy application that still needs it, being run at the same time as the code you write.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gang,

Consider the following scenario if you don't have synchronization:
  • Client A calls read(5)
  • A's thread seeks to location of 5th record
  • Client B calls read(6)
  • B's thread seeks to location of 6th record
  • B's thread reads 6th record

  • Now when A's thread slices back in, the file pointer will actually be pointing to the 7th record, not the 5th!

    So some sort of synchronization is needed, just for reads to work correctly.

    By the way, it gets even worse, since if you don't have any synchronization in your read method, then it will not be honouring your updates / deletes synchronization either - in other words your reads could result in a corrupted database.

    Regards, Andrew
     
    Gang Chen
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Andrew:

    Thanks for the reply. I think you are absolutly right. In other words, I will
    somehow synchronize all the methods in Data.java. But my worry is that the performance will take a hit due to synchronizations. Or maybe I just need to synchronize some blocks of code within a method or maybe just the object of RandomAccessFile?

    Could you throw more thoughts to enlighten me?

    Thanks again.
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 12007
    215
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Gang,

    If you synchronize all your methods, you will certainly experience a performance hit, as only one client (thread) will be able to run any method in the Data class at any given time.

    You will get much better concurrency if you use synchronized blocks around only those particular sections of code that must be limited to only a single thread - disk operations and lock operations (you might also want to think whether these would need the same mutex or whether they can use different mutexes). Even within those, you might want to think of how you can minimize your synchronized blocks - for example reading an entire record in one step, rather than reading multiple small fields.

    Regards, Andrew
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic