Java class annotated with non thread safe

276 Views Asked by At

I have a java class that represents AWSSecretManager client with annotation @NonThreadSafe, am I allowed to use Singleton to get a single instance of that class?

1

There are 1 best solutions below

0
Cayman On

NonThreadSafe means that if different threads are accessing the value at the same time you can get inconsistent results. that annotation clarifies that the class is not thread-safe.

If you try to create a singleton of the class but different threads are accessing to it at the same time it makes no difference

Remember that a singleton is only one instance globally. If you create a singleton in order to access the non thread safe class it means that only one instance would access the class.

So... if you are not using threads you are safe to use the class. If you are using threads make sure to access in a way that you don't work on the same data simultaneously