EmailSurfer
asked on
Synchronize doPost, doGet?
Hello,
Could anyone advise if the doGet, and doPost methods should be declared as Synchronized in the servlet classes to ensure the servlets are use efficently?
Thanks
Could anyone advise if the doGet, and doPost methods should be declared as Synchronized in the servlet classes to ensure the servlets are use efficently?
Thanks
ASKER
Thanks
In my J2EE books the examples which follow a MCV pattern, do not use the synchronized keyword in the servlets.
Do you think most people donot use synchronized in servlets, or are there particular only circumstances where we would use the synchronized keyword in a servlet?
In my J2EE books the examples which follow a MCV pattern, do not use the synchronized keyword in the servlets.
Do you think most people donot use synchronized in servlets, or are there particular only circumstances where we would use the synchronized keyword in a servlet?
Hi,
There is a very good explaination about synchronize or thread-safe servlets here
http://www.jguru.com/faq/view.jsp?EID=150
the above approach i mentioned is quite common and generally it is also preferred to do synchronize on the shared object level. In books u may find this as a matter of lines and not as a topic. But using synchronize in servlets is not new!
regards,
fargo
There is a very good explaination about synchronize or thread-safe servlets here
http://www.jguru.com/faq/view.jsp?EID=150
the above approach i mentioned is quite common and generally it is also preferred to do synchronize on the shared object level. In books u may find this as a matter of lines and not as a topic. But using synchronize in servlets is not new!
regards,
fargo
synchronizing on "this" is not a good idea, your servlet wont be able to response to more than one request at a time, its not even a way to make your servlet run efficiently. what do you mean of efficient ?
hi hoomanv, what do u think could be any other way to do synchronise if not on "this". I agree and i already stated that the share object level sync is more better. I never said it is efficient, but depends upon context it is useful. The link i posted also clears this topic.
fargo
fargo
yes there is other way. your servlet needs to implement the empty interface javax.servlet.SingleThread Model and nothing more is required. this will guarantee that no two threads will simultaneously execute your servlet's service() method, by nstantiating a new servlet for each simultaneous request. thus it is not and efficient way either.
I think you have to do proper OO design before attempting to use whether synchronization or SingleThreadModel. and where ever your thread needs to be safe, its better to synchronize on an local variable (simply called a mutex or mutual exclusion that is shared among all threads) not on the entire servlet instance. accurately identify which code block truly needs to be synchronized as a small synchronize block would be more specific to what you want to do. and at last if your load time minimal in the case that your synchronized code is more than nonsynchronized you have the choice to use SingleThreadModel and take the responsiblity.
I think you have to do proper OO design before attempting to use whether synchronization or SingleThreadModel. and where ever your thread needs to be safe, its better to synchronize on an local variable (simply called a mutex or mutual exclusion that is shared among all threads) not on the entire servlet instance. accurately identify which code block truly needs to be synchronized as a small synchronize block would be more specific to what you want to do. and at last if your load time minimal in the case that your synchronized code is more than nonsynchronized you have the choice to use SingleThreadModel and take the responsiblity.
>> you have the choice to use SingleThreadModel and take the responsiblity.
Since 2.4 of the Servlet spec, SingleThreadModel is deprecated, so it's recommended you handle synchronization yourself
Since 2.4 of the Servlet spec, SingleThreadModel is deprecated, so it's recommended you handle synchronization yourself
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry Tim, i posted the same line. Didn't refresh the page.
fargo
fargo
I would do the following in a servlet to acheive synchronisation
/**
* @param arg0
* @param arg1
* @throws javax.servlet.ServletExcep
* @throws java.io.IOException
*/
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
synchronized (this)
{
servletBodyCodeOrMethod(re
}
}
/**
* @param arg0
* @param arg1
* @throws javax.servlet.ServletExcep
* @throws java.io.IOException
*/
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
Hope it helps.
fargo