File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can someone give a good example for ThreadSafe servlet

 
niroshan iranga
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone,

can someone give a good example for ThreadSafe servlet.I would like to get some idea about by undestanding the real world example

thank you
 
amar verma
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is a simple servlet that is not thread-safe.

public class SimpleServlet extends HttpServlet
{
//A variable that is NOT thread-safe!
private int counter = 0;

public void doPost()
{
for (int c = 0; c < 10; c++)
{
resp.getWriter().println("Counter = " + counter + "<BR>");
Thread.currentThread().sleep((long) Math.random() * 1000);
counter++;
}
}
}

The variable counter is an instance variable,
The problem here is that our servlet container is multithreaded and
shares single instances of servlets for multiple requests.
, only one place in memory is allocated for this variable,
and it is shared between all threads that intend on executing this same class instance.
So defining your variables as instance variables doesn't sound like a good idea? In output we fail to get the results we desire.

To ensure we have our own unique variable instance for each thread, we simply move the declaration of the variable from within the class to within the method using it,,, The difference is that, for each call to the method, a new variable is created
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"defining your variables as instance variables doesn't sound like a good idea? In output we fail to get the results we desire..."

What if I declare it as an instance variable but initialize it in doPost... i.e.

public class SimpleServlet extends HttpServlet
{

private int counter;

public void doPost()
{
counter=0;

for (int c = 0; c < 10; c++)
{
resp.getWriter().println("Counter = " + counter + "<BR>");
Thread.currentThread().sleep((long) Math.random() * 1000);
counter++;
}
}
}

Now won't it be thread safe?
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry!!.. I realized now..It will still not be thread safe....
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi niroshan,
It is possible to have a threadsafe servlet if the servlet implements SingleThreadModel. If this is done, then there is as single instance of servlet to service each request of client. It means there have to be n number of servlet instances to serve n client request.We are aware that the servlet instantiation is an extensive activity and for each instance memory is required, as a result we will run out of memory.

One way to achieve thread safety is to put your shared code in synchronized block/method. But by doing this we make the other threads wait for to acquire lock, while a thread is accessing the shared code and it will affect the servlet performance.

So it is better that we desing the servlet in such a way that the class variables are some constants and the variable that change values are defined inside methods.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic