Solved

serving some clients concurrently

Posted on 2000-05-14
8
182 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best (free) software to access postgres db (java) 1 32
create a gui in perl 3 70
Java Timer (static) 9 36
use lov values 2 29
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

18 Experts available now in Live!

Get 1:1 Help Now