Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can synchronization completely guarantee thread safety?

 
Ailsa Cape
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everyone!
After confirming the Non-Networked mode design, I got a problem again! Many friends in my university don�t make me fully understand it, so I have to appeal for aid in this forum. A friend told me I would get the best answers after I had written my search key words and entered the member id �47425� in search page. It is true. That�s terrific!

After I studied several useful searching posts this way, I now know the locks are not intended to handle thread safety of my file IO operation which confused me before. That is to say using synchronized modifier on the update and read methods is necessary to ensure thread safety.

My question is a) Will such case appear?


b) Should I use all synchronized modifiers only in LockManager and RemoteData classes, the latter of which in the server side is an adapter of Data class, instead of adding those modifiers in Data class? By the way, my assignment is B&S.

Appreciate any advice, Thanks in Advance!
Regards, Ailsa Cape
[ August 12, 2005: Message edited by: Ailsa Cape ]
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

case a can not appear if you synchronize all actions that require setting the pointer on the same entity, and if you don't leave this synchronized context during the important operations (e. g. don't leave it between seeking and the actual deleting).

About b: The class that has the implementation for the actual file access should not not allow the worst-case-szenario you described. Thus make sure in that implementation that it is thread-safe on that level already, rather than documenting that it is not and providing the synchronization elsewhere. That is less error-prone.

Typically, you will often have to synchronize on the same entity in the implementation, but not in the entire method. Here is an example for an implementation of the find-method:

Kai
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ni hao Ailsa,

Originally posted by Ailsa Cape:
A friend told me I would get the best answers after I had written my search key words and entered the member id “47425” in search page. It is true. That’s terrific!


Thanks

I agree with Kai regarding question A, however I would add one extra disclaimer: using synchronized methods only works as long as you only have one instance of the class. If you had more than one instance of the Data class, then in each instance the method would be sychronized on that particular instance - the other instances can continue running quite happily. Whether that makes a difference or not depends on how many instances of your RandomAccessFile you have.

By the way - you would probably get slightly better concurrency by changing your synchronization. Instead of having synchronized methods, you could have synchronized blocks. To give you an example of where this makes a difference, when reading a record you typically have to:
  • locate the record in the file
  • read the bytes
  • confirm the record is not deleted
  • convert the bytes into the correct format (an array of ASCII Strings)
  • Only the first two steps need to be synchronized (treated as atomic). If you have the entire method synchronized, you will block any other threads from reading from the file while your thread is doing the validation / conversion.

    Originally posted by Ailsa Cape: b) Should I use all synchronized modifiers only in LockManager and RemoteData classes, the latter of which in the server side is an adapter of Data class, instead of adding those modifiers in Data class?


    Check how your instructions have been written - they have been specifically engineered around the Data class being a standalone class (or a Facade to several "invisible" classes that do the work behind the scenes). So the Data class or the classes that it calls must be thread safe. You cannot delegate the thread safety to classes that call Data.

    Originally posted by Ailsa Cape:
    By the way, my assignment is B&S.


    You don't need to mention this - all the assignments have similar issues to the ones you discussed in this post. If, at some later date, you have a question about a particular method signature you have been given, then it will still be irrelevant - another forum member with B&S could have a different method signature while a forum member with URLyBird could have the same method signature.

    Reegards, Andrew
     
    Ailsa Cape
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I really appreciate your replies. Thank you, Kai and Andrew!
    Hi, Kai.

    Originally posted by Kai:
    case a can not appear if you synchronize all actions that require setting
    the pointer on the same entity, and if you don't leave this synchronized context during the important operations (e. g. don't leave it between seeking and the actual deleting).

    It�s reasonable. Could you share more specific ideas about how to synchronize all actions in update method? I can�t figure out a way to treat a series of actions as atomic. And I don�t quite understand the FILE_SYNCHRONIZER variable in your example code. Does this represent the object raf?

    Hi, Andrew. Ni hao, too.

    In server mode, every client in my design will get its own unique instance of Data class and share a single instance of RandomAccessFile class. Would the following codes do the trick?


    Would you like to give me more hints or details to ensure thread-safety in such situation?

    I have just searched one post ThreadsafeDatabase4
    concerning thread safety.In this post, Vlad launched a great discussion about Database thread safety. You gave a lot of excellent comments on a set of ThreadsafeDatabase classes. In the class ThreadsafeDatabase4, you didn�t use the means of synchronizing blocks. I wanna ask if two threads separately run in read and update methods simultaneously, will this make the underlying file pointer changed unexpectedly? Is it due to more than one instance of RandomAccessFile?

    My instructions state that Your data access class must be called �Data.java�, must be in a package called �suncertify.db�, and must implement the DBMain interface. Can I make the Data class be a Fa�ade to several �invisible� classes that do the work behind the scenes?

    As to the synchronization modifiers, do you mean I should make thread safe in the Data class rather than in my RemoteData class?
    Thanks for your well-meaning reminder of my useless assignment version declaration, I will remember and correct it next time. Lucky enough, it won't cause automatic failure.
    I think I am probably not on the right track, please help me!

    Any advice will be appreciated! Thanks in Advance!

    Regards,
    Ailsa Cape
    [ August 13, 2005: Message edited by: Ailsa Cape ]
     
    Kai Witte
    Ranch Hand
    Posts: 356
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hello,


    Could you share more specific ideas about how to synchronize all actions in update method? I can’t figure out a way to treat a series of actions as atomic. And I don’t quite understand the FILE_SYNCHRONIZER variable in your example code. Does this represent raf object?

    it is not important for your problem, but since you asked: FILE_SYNCHRONIZER is a private static final Object. In my particular case I can not synchronize on the RandomAccessFile, because it is not final.

    "atomicity" in the meaning we need here can be reached by correct synchronizing. For this aspect your SCJP threading skills should be sufficient.

    Kai
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 12014
    220
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Ailsa
    Originally posted by Ailsa Cape:
    In server mode, every client in my design will get its own unique instance of Data class and share a single instance of RandomAccessFile class. Would the following codes do the trick?
    Well, apart from the fact that this code won't compile , I think you are showing the right concepts there, so it should be OK.
    Originally posted by Ailsa Cape:
    I have just searched one post ThreadsafeDatabase4 [...] In the class ThreadsafeDatabase4, you didn’t use the means of synchronizing blocks. I wanna ask if two threads separately run in read and update methods simultaneously, will this make the underlying file pointer changed unexpectedly? Is it due to more than one instance of RandomAccessFile?
    From memory I was working on the assumption that there could only be one instance of ThreadsafeDatabase4, otherwise the synchronized methods would not work. As long as there is only one instance, then no matter how many threads there are, they will all synchronize on the same object.
    Originally posted by Ailsa Cape:
    My instructions state that Your data access class must be called “Data.java”, must be in a package called “suncertify.db”, and must implement the DBMain interface. Can I make the Data class be a Fa�ade to several “invisible” classes that do the work behind the scenes?
    Yes you can. If you do this, you must ensure that clients only call your Data class. You might want to consider changing your access level of the classes / methods behind the Fa�ade to restrict access.
    Originally posted by Ailsa Cape:
    As to the synchronization modifiers, do you mean I should make thread safe in the Data class rather than in my RemoteData class?
    That is my reading of the instructions - the Data class must be written in such a way that it meets all the contractual obligations of the interface. You cannot rely on client code to meet your contractual obligations.

    Regards, Andrew
    [ August 13, 2005: Message edited by: Andrew Monkhouse ]
     
    Ailsa Cape
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you again, Andrew and Kai!

    Hi, Andrew
    Your answers finally dawned on me. You are the first person who let me completely understand the importance of Database thread safety and the implementation of treating a series of actions as atomic. Thanks a million!

    I try my best to show the following particular implementation more generic in order to not break any rules in this forum. If you think it is unadvisable to expose many details, I will apologize for that and you can erase those pseudo codes.

    Because in Net-work mode, many clients� requests would cause RMI server to start many threads and generate many instances of Data class (in my design). In such a case, those threads would separately run in many instances of Data class�s update methods simultaneously. According to your instruction,

    Originally posted by Andrew:
    using synchronized methods only works as long as you only have one instance of the class.

    So I think the update method in Data class is unnecessary to be synchronized, is it right?

    It is more significant whether my Client class matches your �must� requirements, because I must attach greater importance to your �must� statement and adhere to it. I wish you could give me more hints about how to change my access level of the classes behind the Fa�ade to restrict access for I don�t quite catch on it.

    Originally posted by Andrew:
    If you do this, you must ensure that clients only call your Data class. You might want to consider changing your access level of the classes / methods behind the Fa�ade to restrict access.


    By the way, it is my honor to have the opportunity to read Your great book. I will purchase one even if I would not take part in the SCJD Exam since it can enormously promote my level of designing Java program in the future. Except this book, would you like to introduce other Java books written by you to me?

    Thanks in advance!
    Ailsa Cape
    [ August 14, 2005: Message edited by: Ailsa Cape ]
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 12014
    220
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Ailsa,

    I wish you could give me more hints about how to change my access level of the classes behind the Fa�ade to restrict access for I don’t quite catch on it.


    This is something that you already know, but may not have had need to use before.

    Think about the packages you would possibly be using for your assignment. Presumably one for the database, possibly another for the network, possibly another for the GUI, and so on.

    If the methods in your Data class have public access then they can be accessed by any class in any other package, which is probably what you want.

    Conversely, if your methods in the other classes in the database package (the ones behind the fa�ade) have default access then they can only be accessed from other classes within your database package. In other words, the classes in the network package or the gui package will be unable to access them.

    would you like to introduce other Java books written by you to me?


    That is the first book I have written.

    Regards, Andrew
     
    Ailsa Cape
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi, Andrew
    Your answers are very much appreciated. Now I totally catch on the problems which puzzled me before. Thank you again!

    Regards, Ailsa Cape
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic