FieldDictionary thread safety

Hi,

My app creates and loads a single data dictionary on startup and has multiple threads using it afterwards for field list decoding. May I know if the read-methods of the data dictionary are thread-safe? In particular, my concern is these methods:

FieldDictionary.getFidDef()

FieldDictionary.expandedValueFor()

FidDef.getName()

FidDef.getOMMType()

I am using RFAJ 8.1.0 and Java 8.0

Thanks,

CM

Best Answer

  • wasin.w
    wasin.w admin
    Answer ✓

    Hello @CM Wong

    I have re-checked with the RFA Java Developer Guide, those utilitiy classed are used for OMM data in single-thread environments. Multi-threaded environment Applications should incorporate their own locking mechanism.

    image

Answers

  • Hello @CM Wong

    Based on the RFA Java Developer Guide document section 7.9.4 Thread Safety,

    All interfaces are thread-safe at the class level (static methods - if any). In addition to that, some interfaces are thread-safe at the object level. The main reason for not support the object-level
    thread-safe for all methods is the performance concern.

    If an application developer finds out that they need access to the same object from multiple threads, then they need to
    protect this object with an explicit locking mechanism.

    Note:

    • Class-level thread-safety means that static methods (if any) can be called from multiple threads at the same time, and that if there are any class-wide resources (i.e., static data members) then access to these resources from class instances is properly synchronized.
    • Object-level thread-safety means that any non-static methods implemented by the class can be called on the same object (class instance) from multiple threads at the same time.
    • Dear Wasin,

      Thanks for your reply.

      However, since the data dictionary is heavily used in the decoding of field list, if I put a lock at every of its methods, that will defeat the purpose of using multiple threads for handling of OMM messages.

      I just wonder if I can call the mentioned methods from multiple threads safely since all of the methods are read-only (i.e. they won't modify the data dictionary)

      Regards,

      CM

    • Wasin Waeosri

      I have no problem with the developer guide. But since all of the methods I call don't modify the field dictionary, I just wonder if they can be safely called from multiple threads.

      Please take a look at the javadoc for HashMap. It mentions:

      If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.

      In fact, if all of the threads calling a hash map don't modify the map at all, they don't need to do any locking.

      This is may question for field dictionary.

      Thanks,

      CM

    • Hello @CM Wong

      I have checked with the development team. You can use the following data dictionary read methods without any locking mechanism implementation on your side:

      • FidDef.getName()
      • FidDef.getOMMType()

      However, you need to implement locking mechanism when using FieldDictionary.getFidDef() and FieldDictionary.expandedValueFor() methods in multi threads application. The reason is these methods have additional logic to return the values, so the locking mechanism is required.

      Please note that FieldDictionary and FidDef classes won’t work
      like HashMap as these classes have methods which has additional business
      logic.

    • Hello @Wasin Waeosri

      That's good.

      Thanks,

      CM