Synchronization and thread safety

From WebLichtWiki

Jump to: navigation, search

If your application Resource is created as Singleton (see Instantiation of resources for details) you have to make sure it is thread-safe. It means that all its shared objects and calls to its shared object are either thread-safe per se, or are wrapped in synchronized methods or blocks.


As an example, consider References Identifier Service. Here the Resource tool, ReferencesTool object is shared among the client. The ReferencesTool object is thread-safe: it provides only read methods and it doesn't store any object specific information in its instance fields. No synchronization is required here.


Another example, Named Entities Recognizer Service, also uses a tool that is shared among the clients, NamedEntityTool object. In its instance field, the NamedEntityTool object stores information specific to each document it processes. To make it thread-safe, the implementation has to specify its process() method as synchronized. This makes the whole Resource thread-safe, now client requests can share the Resource without any concurrency problems.