This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes can someone give a good example for ThreadSafe servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "can someone give a good example for ThreadSafe servlet" Watch "can someone give a good example for ThreadSafe servlet" New topic
Author

can someone give a good example for ThreadSafe servlet

niroshan iranga
Greenhorn

Joined: Apr 28, 2004
Posts: 19
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

Joined: Jun 09, 2005
Posts: 19
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


Amar Verma<br />---------------<br />SCJP 1.4<br />SCWCD1.4 (Preparing)<br />Dont even follow the leading path, create your own path and leave the trail.
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
"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?


SCJP 1.4 : 91%
SCWCD 1.4 : 95%
SCBCD 1.3 : 95%
SCJP 6 Upgrade : 95%
Next SCBCD 1.5
Man is not finished when he is defeated. He is finished when he quits.
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
Sorry!!.. I realized now..It will still not be thread safe....
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
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.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
 
Don't get me started about those stupid light bulbs.
 
subject: can someone give a good example for ThreadSafe servlet
 
Similar Threads
what is a factory method?
Regarding Method Shadowing
getServletPath, getPathInfo
cld some one give me real world examples..
SingleTon class