Can we write synchronize service method in servlet?If no why

M

Mahesh

Hi Guys
I am new to this group . I working on J2ME and learning Servlet .
I have question on synchronization of servlet.If not whay we cannot
write .
 
M

Mahesh

service method i.e
public synchronize void service (HttpServletRequest
req,HttpServletResponse res)
 
C

chris brat

I dont think that adding the synchronized keyword will cause you a
problem with either compilation or running - it didn't in the simple
example class I wrote.

But why would you want to do this? it would kill performance.

The functionality seems to describe the behaviour of the
SingleThreadModel which was deprecated.
 
T

Thomas Fritsch

Mahesh said:
Hi Guys
I am new to this group . I working on J2ME and learning Servlet .
I have question on synchronization of servlet.If not whay we cannot
write .
I am not sure about what you mean with "synchronization of servlet". May
be you mean the SingleThreadModel as specified in
<http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/SingleThreadModel.html>.
You can use it like this:
public class MyServlet extends HttpServlet implements SingleThreadModel {
...
}
 
J

Juha Laiho

chris brat said:
I dont think that adding the synchronized keyword will cause you a
problem with either compilation or running - it didn't in the simple
example class I wrote.

But why would you want to do this? it would kill performance.

The functionality seems to describe the behaviour of the
SingleThreadModel which was deprecated.

SingleThreadModel is quite a different beast than synchronized service
method. Ok, both guarantee, that there is just one thread running
in the service method of a single servlet instance.

Then to the difference: if you use SingleThreadModel, the servlet container
will be allowed to create multiple instances of your servlet class
to allow concurrent processing of multiple requests (which would be
a big no-no, if you chose the SingleThreadModel to protect some fragile
constructs you access from within the service method). If, on the other
hand, you synchronize the service method (and do not use the
SingleThreadModel), the container will be limited to just one instance
of any given servlet - thus permitting just one request running in
the service method of the servlet. However, this would be a major
performance bottleneck; it'd be better to use some more fine-grained
locking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top