Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multithreading

 
Mansor Mohammed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mansor Mohammed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Mansor Mohammed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks for response ..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansor Mohammed wrote:I don’t have any data sharing between threads, all the threads will call ‘String encrypt(String number)’ which will return encrypted number.


If that method uses only local variables, and if none of those variables refer to objects shared with other threads, then it is safe.

My only confusion is if thread1 calls this method is there a change of getting thread2 response to thread 1?


No.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic