GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multithreading" Watch "Multithreading" New topic
Author

Multithreading

Mansor Mohammed
Greenhorn

Joined: May 15, 2012
Posts: 3


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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 15, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 15, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
GeeCON Prague 2014
 
subject: Multithreading