> Are there any examples online to see the synchronization done in servlet?

A better strategy would be to take a class or read a book on proper
concurrency control.  I can recommend "Concurrent Programming in Java" 2nd ed
by Doug Lea.  This book is fairly difficult, but if you can get past the
chapter 1 (out of 4), then you will have a very good understanding of the
issues of multithreading and the parallels with a Web container should be
fairly obvious.

> Singlethreadmodel will give take care of container which has servlet in it.
> that means it will have only 1 thread activating the service() method? if so
> how does the next incoming thread get the service() handle? Please let me
> know exactly what happens with this model? is it internally implemented, or
> we have to write it?

Take my advice and *forget* STM.  It is deprecated in the latest servlet
spec and was of debatable value to begin with.

Instead, learn about proper sync. control (as I mentioned above).  Here are
a heuristics that I use:

  * synchronize only critical code sections of your servlet

    Do *not* synchronize the whole service (or doGet, doPost) method.
    "Critical code" sections are those that affect (modify) shared data
    (instance vars, class vars, Session attrs, and Context attrs).
    For example:

public class MyServlet extends HttpServlet {
   private int counter;
   public void doGet(...) {
     ... // do some work
     // update counter
     synchronize (this) {
       counter++;
     }
     ... // do more work
   }
}
    Notice that is very little "critical code".  Multiple threads executing
    this doGet method will only have to "wait" (if neccessary) to update
    the counter variable *not* for the whole method.

  * use read-only instance and class variables

    The biggest mistake novice servlet developers make is to have lots of
    instance variables that store "state".  Sometimes, this is unavoidable.
    But consider doing the following: (a) collect these variables into a
    single (or a few) objects that the servlet references and make *this*
    class thread-safe, (b) only use instance/class vars for read-only
    information (such as the servlet init. parameters).

  * use immutable objects where appropriate

    Immutable objects (such as String and primitive wrappers) are inherantly
    thread-safe becuase you cannot change the data (by definition).

  * use resources (objects) that are properly designed for multithreading

    The perfect example of this is a DB connection pool (or a JDBC DataSource).
    CPs must be *built* to be thread-safe because they are will be used
    extensively by multiple threads.

This is just a starting point.

HTH,
Bryan


+-----------------------------------+------------------------------------------+
| Bryan Basham                      | What we are today comes from our         |
| Java Courseware Developer         | thoughts of yesterday, and our present   |
| Sun Services                      | thoughts build our life of tomorrow:     |
|   Phone: 1-303-272-8766 (x78766)  |    our life is the creation of our mind. |
|  E-mail: [EMAIL PROTECTED]     | If a man speaks or acts with a pure      |
| Address: 500 Eldorado Blvd        | mind, joy follows him as his own shadow. |
|          MailStop: UBRM05-135     |  -- The Dhammapada (verse 2)             |
|          Broomfield, CO 80021     |     (trans. Juan Mascaro)                |
+-----------------------------------+------------------------------------------+

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to