• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multi thread servlets and the Server applications they call.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes sense. Thank you.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic