Question

It says since Struts 2 actions beans are initialized each request they are thread safe. But what if several threads use this bean within the same HTTP request? So it is not really thread safe right unless we wrote the bean in a thread safe manner?

Below is from struts 2 docs says when compare Struts 1 and Struts 2,

Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues

struts 2 documentation

My dought is even Struts 2 Action objects are instantiated for each request still its not thread safe right?

Was it helpful?

Solution

At my point of view a web application is designed using servlet technology. Struts2 support servlet specification at least 2.4. A web server communicates to the client using HTTP protocol and it manages threads. And you know that no more than one thread is created per request by container. The framework doesn't create any threads except BackgroundProcess and it looks like it's doing it in the thread safe manner. You are not responsible to manage action beans, the framework is doing it for you. So, if it claims that it's thread safe you can trust it, or compromise if you have an evidence to opposite. According to this info you can conclude that you can use managed beans that are thread safe.

OTHER TIPS

For each request new instance of Action class is created. So there is no thread safety issue with struts 2 action class.

yes it is thread safe.

Struts1 Action Classes are Singleton and not thread safe, that makes extra care on developer side to avoid any side effects because of multithreading. Struts2 action classes gets instantiated per request, so there is no multithreading and makes them thread safe.

Blockquote

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top