Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

serving some clients concurrently

Posted on 2000-05-14
8
Medium Priority
?
193 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
  • 4
  • 4
8 Comments
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 120 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
Independent Software Vendors: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

885 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