File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Threads in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Threads in servlet" Watch "Threads in servlet" New topic
Author

Threads in servlet

verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
I have a web application whose purpose has been to display data from the database. I now need to add the ability for users to enter data. I have a JSP with a button to add an item to a list. When this button is pushed, I popup a new window, allowing the user to enter the required information to create a new item. When the user is finished, he will click the Add button at the bottom of this popup window. At this time, I want the JSP to submit to a servlet. This servlet will be used in this case (the user adds a new item) and also when the user chooses to modify an existing item. After adding/modifying the item as appropriate, the page will return to the original page.
More information on this servlet: the doGet() and doPost() both call a performTask() method. The performTask() method gets several parameters from the request, including an indicator if the item was added or edited. Depending on this indicator, the item will either be added to the Vector of items or replace the original item in the Vector. This Vector is then saved to a JavaBean, and the servlet will pass the control back to the original JSP so that it can close and refresh the parent. The Vector will be saved to the database at a future point.
I have not used servlets much in this context, and want to make sure that it will be thread safe. We have a couple hundred users on this system, and I need to make sure that the information that is entered for each item is saved correctly (no problems due to threading). Do I need to implement the SingleThreadModel? I've done some searching on JavaRanch about the SingleThreadModel but have found conflicting answers.
Thanks in advance for your help.
Sri Basavanahally
Ranch Hand

Joined: Oct 07, 2003
Posts: 75
Hi Verduka !
You can makes this Servlet implement the SingleThreadModel. But, the downside to that is slow performance. If this is Ok with you, go ahead. The general recommendation is to design applications so that they do not need to implement the SingleThreadModel.
In addition, you may want to see if there is a narrower scope of statements that you can "synchronize". This will help improve performance.
-Sri


UP THE IRONS !
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Try as much as you can to not use this interface:
1o) Do not use instance variables. Instance variables are not thread safe (unless you implement SingleThreadModel). Instead, use local variables. Local variables are always thread safe.
2o) Do not use static variables. Static variables are *not* thread safe even if you implement the SingleThreadModel interface.
HTH


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Thanks for your replies!
If I don't make the servlet implement the interface, will my servlet be thread-safe IF I don't use static or instance variables (only local variables)? What about the step where I add the item to a Vector in the JavaBean which is stored in the session? Is this part thread-safe? When is it necessary to wrap statements in "synchronized" block?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
session is not thread safe, so my take here is that you should synchronize access to that. It can happen that once you're iterating over your vector, another requests is removing an element from it. Then you might have problems, or unexpected behaviour.
So,try to have a minimal synchronized code block.
synchronized(yourVector)
{
do what you need t o
}
hope this helps
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

vectors are synchornized.


Groovy
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
I'm posting my servlet code so it can be identified as thread-safe or not:
performTask() called by both doGet() and doPost():
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Pradeep Bhat:
vectors are synchornized.

Yes, but session scope is not thead safe.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads in servlet
 
Similar Threads
Mix Javascript & JSP...
Popup window not working
Focus in a JPopupMenu
[Resolved] Show Popup then Refresh Form
open popup window and insert chinese