Struts 1 Action 类中的同步实例方法意味着什么?
我想检查一下在 Struts 1 Action 类中拥有同步实例方法意味着什么?
像这样的东西
public synchronized String checkAction(){ ... }
i would like to check what it means to have a synchronized instance method in a Struts 1 Action class?
Something like
public synchronized String checkAction(){ ... }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着 checkAction 方法将通过封闭对象的内部锁(Action 类对象的锁)进行同步。因此一次只有一个线程能够访问 checkAction 方法。
在 Struts 1 中,Action 类不是线程安全的。因此多个线程(例如,服务多个请求)将访问 Action 类的同一个实例。方法是否需要同步?我不确定 - 只有你可以通过方法中发生的情况来判断。只要它不访问 Action 类的实例变量或执行一次只能由单个线程完成的操作,则可能不需要同步。
It means the checkAction method is going to be synchronized by the enclosing Object's intrinsic lock (The Action class Object's lock). So only one thread at a time will be able to access the checkAction method.
In Struts 1 the Action class is not thread safe. So multiple threads (e.g. servicing multiple requests) will access the same instance of the Action class. Does the method need to be synchronized? I'm not sure - only you can tell by what's happening in the method. So long as it isn't accessing instance variables of the Action class or doing something which can only be done by a single thread at a time, then synchronization is probably not necessary.