Solved

serving some clients concurrently

Posted on 2000-05-14
8
180 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 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now