question

Upvotes
Accepted
1 0 0 3

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

treprfarfa-apimulti-threading
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
Accepted
11.5k 16 7 10

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.


capture.png (31.1 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
11.5k 16 7 10

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.
    icon clock
    10 |1500

    Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

    Upvotes
    1 0 0 3

    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

    icon clock
    10 |1500

    Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

    Upvotes
    1 0 0 3

    Hi @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

    icon clock
    10 |1500

    Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

    Upvotes
    11.5k 16 7 10

    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.

    icon clock
    10 |1500

    Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

    Upvotes
    1 0 0 3

    Hello @Wasin Waeosri

    That's good.

    Thanks,

    CM

    icon clock
    10 |1500

    Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

    Click below to post an Idea Post Idea