简体   繁体   English

Singleton类线程中的方法是否安全?

[英]Are methods in a Singleton class thread safe?

Is Concurrent method access in singleton class thread safe? 单例类线程中的Concurrent方法访问是否安全?

I am using Spring framework (MVC) with the default scope as Singleton. 我使用Spring框架(MVC),默认范围为Singleton。

For example, if the controller of my web application is a Singleton class, are the methods declared to access Model/Business/DB classes thread safe, if accessed by multiple threads at the same time? 例如,如果我的Web应用程序的控制器是Singleton类,那么声明访问Model / Business / DB类的方法是否是线程安全的,如果同时由多个线程访问? How about calls to DB for database access/update from these methods in the Controller? 如何在Controller中从这些方法调用数据库访问/更新数据库?

Need guidance and I aprreciate any help/suggestions for this issue. 需要指导,我会对此问题提出任何帮助/建议。

No, Singleton does not guarantee thread safety. 不,Singleton不保证线程安全。 You have to manage that yourself. 你必须自己管理。

The best way to do it is to make Singleton's stateless. 做到这一点的最好方法是让Singleton无国籍。 If there's no shared state you're fine. 如果没有共享状态你没事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM