Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi thread servlets and the Server applications they call.

 
Cindy Moncsko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the issues with servlets is that class's field variables are not thread safe. One of the suggestions I found was to change those variables to member variables. That got me thinking. Is this also an issue with the field (class instance) variables of applications called by that servlet? If so that complicates things quite a bit.
Thank you for your help.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables of the servlet are not thread safe. Implement SingleThread model if ur instance variables are not thread safe.
 
Cindy Moncsko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize "Instance variables of the servlet are not thread safe", but how about the "instance variables" of classes called by the servlet.
Say I have an instance of class MyApp created in, and therefore local to, a servlet Method, . Is that class thread safe? Now I have an instance of class MyApp stored as a field (i.e. class) variable. Is that not thread safe, since concurrent calls could reference that MyApp instance?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class created within the service method(or doget,dopost) is local to that method & is thread safe since it can be accessed within that method.
 
Cindy Moncsko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes sense. Thank you.
 
Stephen Peterson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cindy,
See if this sounds reasonable:
Local variables are thread-safe, member variables are not. But if the member variable is of a class that is instantiated and referenced only as a local variable of a servlet, then you should be okay.
Thread-safety of local variables results by giving each thread a private copy of it's own local variables. Thus, the object instantiated and saved to such a variable will never see more than one thread of execution.
Steve Peterson
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above statement of mine holds good if the local object is not passed to some method or assigned to some variable which can be manipulated by more than one thread. For e.g. assigning local variable to instance variable.
class servlets
{
A a;
service()
{
A aa = new A();
a =aa;
}
}
 
arun sahni
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can synchronize the block if you are using class variable.
The single thread model is not very good option when you are using db or connection to other server.
 
Julio Carlos
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Cindy:
Synchronize the method, is not good idea if you have a lot of operations inside.
You can use a UserBean to make the work, an make an instance inside the Servlet method, and you get a thread safing method (doGet or doPost).
This is an example code:
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
MyBean instanceBean = new MyBean();
instanceBean.userMethod();
}
}
public class MyBean {
userMethod(){
//make the work
}
}
This is thread safing!!!
Greetings
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!