aspose file tools*
The moose likes Struts and the fly likes Problem in Struts 2 when run multiple threads concurrently Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem in Struts 2 when run multiple threads concurrently" Watch "Problem in Struts 2 when run multiple threads concurrently" New topic
Author

Problem in Struts 2 when run multiple threads concurrently

Vivekanandan Anandan
Greenhorn

Joined: Mar 10, 2010
Posts: 3
How to design and code my application in Java to allow multiple client threads to run concurrently throughout the application without interfering with one another?


Now, i hit problem when i try to send 2 threads concurrently, and when second thread is coming in, it overwrites the variable instance value which belong to first thread. So, the first thread will get a wrong value from the variable instance to proceed.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure what you're asking. Struts 2 creates an action-per-request--what instance values do you believe are being modified by two separate threads? What kind of threads are modifying them?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 645
Keep in mind that if you're testing this by running multiple browsers on the same client computer, depending on the browser it can look to the server like requests from the same client, and therefore belonging to the same session.
Vivekanandan Anandan
Greenhorn

Joined: Mar 10, 2010
Posts: 3
I am using a Action called SamleAction.java

Declared a static variable count and incrementing it when ever the method is called.

in logging the references of request and response you can see the following


Start--01--StrutsRequestWrapper@867ca8-->ResponseFacade@169b9e3
End--01--StrutsRequestWrapper@867ca8-->ResponseFacade@169b9e3
Start--02--StrutsRequestWrapper@872ebe-->ResponseFacade@169b9e3
End--02--StrutsRequestWrapper@872ebe-->ResponseFacade@169b9e3
Start--03--StrutsRequestWrapper@1b593d2-->ResponseFacade@169b9e3
End--03--StrutsRequestWrapper@1b593d2-->ResponseFacade@169b9e3
Start--04--StrutsRequestWrapper@f663e4-->ResponseFacade@169b9e3
Start--04--StrutsRequestWrapper@f663e4-->ResponseFacade@169b9e3
End--04--StrutsRequestWrapper@f663e4-->ResponseFacade@169b9e3
End--05--StrutsRequestWrapper@f663e4-->ResponseFacade@169b9e3

Start--06--StrutsRequestWrapper@152db16-->ResponseFacade@169b9e3
End--06--StrutsRequestWrapper@152db16-->ResponseFacade@169b9e3

Here when the method is called consecutively you can see the "Start" line two times with same request and response objects.

the response for the line 5 is not received
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Vivekanandan Anandan wrote:Declared a static variable count and incrementing it when ever the method is called.


You either synchronize yourself or use an AtomicInteger and its incrementAndGet() method.


OCJP 6 (93%)
Vivekanandan Anandan
Greenhorn

Joined: Mar 10, 2010
Posts: 3
Thanks for your reply Lorand.

But the thing here is for line 4 and 5 the references for request and response were same.

That is, when thread for line 5 is coming in, it overwrites the variable instance value which belong to thread line 4 .

as because the line 4 will get a wrong value from response and the response for line 5 is not at all received.

(Am sending xml as response as am using flex for front end)
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
The problem is the static variable, which is shared across action class instances (==> shared across threads), this is a classic multithreading issue...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Struts 2 when run multiple threads concurrently