-1

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?

  • Why should this be forbidden? The annotation is [documented](https://jcip.net/annotations/doc/net/jcip/annotations/NotThreadSafe.html) as: _This annotation primarily exists for clarifying the non-thread-safety of a class_. If you are using this singleton from multiple threads it is your job to ensure thread safety. – Thomas Kläger Feb 23 '22 at 22:28

1 Answers1

0

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

Cayman
  • 168
  • 8