Archived Forum Post

Index of archived forum posts


Thread Safety and Best Practices

Apr 21 '16 at 10:14

Question from User:

I am making a JAVA based web app and the app spawns several threads to handle incoming requests. I use crypt2, and every time i use it, I create another new instance of the object. However, it has come to my mind that maybe i can use only one instance of crypt2 as a singleton and use it from various threads.

I believe that all chlikat products are thread safe aren't they?

Is this good idea or should i just stick to the method with spawn own chilkat instance for each threads.


Chilkat is thread safe, and this means that if two threads are trying to simultaneously call a method on a shared object instance, then one will gain access first, and the other thread will wait until the call in the 1st thread completes. Even though the classes are thread-safe, it would likely be better for performance to use a separate object instance for each thread.

Note: For the Socket class, it makes sense that an app would want one thread to be reading while the other is writing. If Chilkat behaves as described above, then this could never happen. There is a solution, and that is to use the CloneSocket method so that your app has two instances of the Socket object. Now one thread can read using instance A, while the other thread can write using instance B (both instances actually use the same underlying socket (TLS/TCP) and internally the thread-safety for the underlying socket is finer-grained). See