I have an application which replace the number in encrypted format stored in the database to another encrypted value with the help of a key. This key will be provided by the ingrian device. All the code required for encryption and decryption is available in Encryption class of commonjar. So my application will invoke this jar for encryption purpose.
Problem: In database I may have more that 50 million records, so I am using threads to run this process parallel. All the threads are accessing Encryption class (local instance for each thread) for encryption . Now my question is even though each thread have its own instance of accessing encryption class there are accessing same jar file, so I there any change of data getting swapped between two threads while encrypting?
Is it thread safe if two are more threads access same jar?
The jar file is completely irrelevant. All that matters is the variables in your Java code. If the threads aren't sharing any data there, then there's no problem. If they are, then you need to use synchronization and/or the classes in java.util.concurrent to ensure proper mutual exclusion and atomicity where needed.
Joined: May 15, 2012
Thus it means whatever code we have in jar file will be thread safe .... even though two thread are calling the same class available in jar??
Mansor Mohammed wrote:Thus it means whatever code we have in jar file will be thread safe
No. Not at all. Not even close. Nothing even remotely like that.
I'm only saying that whether the code comes from the same jar or not has not effect one way or the other on thread safety.
The code in the jar may or may not be thread safe in and of itself. Regardless of how thread safe that code is, the way you use it may or may not be thread safe. But which jar it comes from has no effect on that.
.... even though two thread are calling the same class available in jar??
Multiple threads can use the same class in a thread safe way. They can also use it in a thread-unsafe way.
Joined: May 15, 2012
I don’t have any data sharing between threads, all the threads will call ‘String encrypt(String number)’ which will return encrypted number. My only confusion is if thread1 calls this method is there a change of getting thread2 response to thread 1?