Playframework 1.x静态方法如何以线程安全的方式工作 [英] How Playframework 1.x static method works in a thread safe way
问题描述
可能是一个愚蠢的问题,但是我对学习Play框架1.2.5不感兴趣. 我注意到控制器的动作都是静态方法,我假设每个请求都将在单独的线程中处理.如果是这样,Play框架如何确保这些静态控制器方法是线程安全的?我认为,只要我们不共享控制器方法之间的任何内容,那么我们就很好(?),如果我错了,请纠正我.
Might be a silly question, but I'm noob learning Play framework 1.2.5. I've noticed that the controller actions are all static methods and I assume each request will be processed in a separate thread. If that's the case, how Play framework ensures these static controller methods are thread safe? I think, as long as we are not sharing anything between controller methods then we are good (?), please correct me if I'm wrong.
提前谢谢.
推荐答案
是的,我们还不错.如您所知,HTTP是无状态的,因此请求之间不会共享任何内容.在处理请求时,播放使用ThreadLocal
.每个请求都有其自己的线程,因此您可以完全控制当前请求-响应生命周期.
Yes we're quite good. As you know HTTP is stateless, so nothing will be shared between requests. Play uses ThreadLocal
s while processiong the request. Each request has it's own thread so you have full control of the current request - response life cycle.
如果要在控制器之间共享数据,则可以使用session
对象,但是该对象很小,并且仅接受String
个对象.对于其他数据类型或共享的不同实现,应使用Cache
实现.有关更多信息,请参见会话和
If you want to share data between controllers, you have session
object however it's quite small and accepts only String
s. For other data types or different implementations of sharing, you should use a Cache
implementation. For further information see session and cache documentations.
祝你好运!
这篇关于Playframework 1.x静态方法如何以线程安全的方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!