Java类被注释为非线程安全
我有一个代表 AWSSecretManager 客户端的 java 类,带有注释 @NonThreadSafe,我是否可以使用 Singleton 来获取该类的单个实例?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NonThreadSafe 意味着如果不同的线程同时访问该值,您可能会得到不一致的结果。该注释阐明该类不是线程安全的。
如果您尝试创建该类的单例,但不同的线程同时访问它,那没有什么区别。
请记住,单例只是全局的一个实例。如果您创建一个单例来访问非线程安全类,则意味着只有一个实例可以访问该类。
所以...如果您不使用线程,则可以安全地使用该类。如果您使用线程,请确保以不同时处理相同数据的方式进行访问
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