Solved

serving some clients concurrently

Posted on 2000-05-14
8
190 Views
Last Modified: 2013-11-24
Does a servlet that serves a lot of clients at once have to implement multithreading in it (thread for each client) or the web server is the responsible for managing the clients , which means that the servlet is written simply without threads?
0
Comment
Question by:twg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 30 total points
ID: 2808088
Webserver won't  provide concurrency control.

It is the programmer's responsibility to provide concurrency control;

If You declare Your servlet class by adding the following  text

      implements SingleThreadModel

then it  will automatically take care of  synchronization.


Other wise You have to take card in code for concurrency control.

You can use synchronized blocks  or any other Your own mechanism



From Sun Tutorial on Servlets:-


Servlets can run multiple service methods at a time. It is important, therefore, that service methods be written in a thread-safe manner. For example, if a service method might update a field in the servlet object, that access should be synchronized. If, for some reason, a server should not run multiple service methods concurrently, the servlet should implement the SingleThreadModel interface. This interface guarantees that no two threads will execute the servlet's service methods concurrently.

During a servlet's lifecycle, it is important to write thread-safe code for destroying the servlet and, unless the servlet implements the SingleThreadModel interface, servicing client requests. For more information on writing thread-safe code, please see the Java Tutorial.

By default, servlets written by specializing the HttpServlet class can have multiple threads concurrently running its service method. If, for whatever reason, you would like to have only a single thread running a service method at a time, then in addition to extending the HttpServlet, your servlet should also implement the SingleThreadModel interface. This does not involve writing any extra methods, merely declaring that the servlet implements the interface. For example,

public class SurveyServlet extends HttpServlet
                           implements SingleThreadModel {

    /* typical servlet code, with no threading concerns
     * in the service method.  No extra code for the
     * SingleThreadModel interface.  */
    ...

}


:)

0
 

Author Comment

by:twg
ID: 2808299
My servlet connects to a database and retrieves
some data according to a user's request.
a lot of web clients should be able to login to the servlet and get the data to their browser.
Do I have to add extra code to enable that or, as I read in the paragraph: "By default, servlets written by specializing the HttpServlet class can have multiple threads concurrently running its service method".Does this means without adding additional code?
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2808348

THere are two options.

1. See this code

public class SurveyServlet extends HttpServlet                           implements SingleThreadModel {

    /* typical servlet code, with no threading concerns
     * in the service method.  No extra code for the
     * SingleThreadModel interface.  */
    ...

}

If U declare Your servlet as like this

i.e implements SingleThreadModel,

all will be taken care by servlet engine itselt. It is thread free.


2. Use the database access code in

synchronized(this) {


}

I hope this will help
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:twg
ID: 2809370
Thanks alot Ravindra76. I have one more question that isn't perfectly clear to me-I tried to run my servlet with a few clients (on a local network) and it worked fine with and without adding the "implements SingleThreadModel".
Is this because it's only few clients on a local nt?
Does every client get a thread for itself, which calls the service method of the servlet (which means that there's only one instance of the servlet), or for each client another instance of the servlet is created?

(sorry for asking too many questions but I have to understand these points)
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2809540

1. Your local version may run succesfully with less cleints because

      The time at which they submitted the request and processed request are not
      matched with another client's request submission due to milli second's difference.

      Try with simulate the code with out implements SingleThreadModel by

      allwing more than two clients clicking submit same time ( Use 4 or 5 other windows )

      You will get difference.

      So use  implements SingleThreadModel .

2. Through out the life cycle , only one servlet process is created and each

    request to service() method will create a new Thread.
0
 

Author Comment

by:twg
ID: 2809705
Thanks a lot Ravindra. my last question-
If I use DBConnectionPool manager which manages the connection to the databases between the servlets and the methods in it are synchronized, Do I still have to implement the SingleThreadModel?
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2809858
Then No need.

:)
0
 

Author Comment

by:twg
ID: 2810106
Thanks for all your answers
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question