Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2003, 12:29 AM
Authorized User
 
Join Date: Jul 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default By Default Servlet is Single or Multi-Thread Model

Hi all Friends,

I want to clear my doubts By Default Servlet is Single-Thread Model or Multi-Thread Model.Can any one please explain it.

Thanks & Regards
Bikash
 
Old September 7th, 2003, 12:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

By default servelts are intrinsically multithreaded, which means that a single instance can can be accessed by more than one thread.

Go to www.google.com and search on "servlet multithreded".


Cheers

Martyn
 
Old September 10th, 2003, 01:43 AM
Authorized User
 
Join Date: May 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
By default a Servlet is Multithreaded and to make a servlet singlethreaded one has to implement the SingleThreadModel interface.

Regards

Yashraj Chauhan
Java\J2EE Specialist
Wiley Support Team
 
Old November 10th, 2004, 12:39 AM
Registered User
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

There is no such interface as MultiThreadedModel. Servlets are intrinsically multithreaded. This means a single instance can be accessed by more than one thread.

If the container receives multiple requests for one servlet simultaneously, the service () method of that servlet will be executed concurrently in multiple threads

If a servlet implements the SingleThreadModel interface, the container will not execute the service () method in more than one thread simultaneously.
The servlet container may synchronize access to a single instance of the servlet.
However, servicing requests sequentially seriously hurts performance. To avoid the performance problem, a servlet container may create multiple instances of the servlet class.



Different variables in both models thread safe or not:-
1. Single Thread Model:-
Local variables:- are defined within the body of a method. [Levi] Always thread-safe.

Instance variables:- Instance variables are defined within the class body and are separately assigned to each instantiated object. Thread-safe only for SingleThreadModel

Class or static variables:- Only one copy of a class variable exists across all the instances of the object belonging to the class for which it is declared.
Class variables , or static variables, are shared among all instances of a servlet. NEVER thread-safe
(No, not even if servlet implements SingleThreadModel.)

Request attributes :- Always thread-safe
Session attributes:- NEVER thread-safe
Context attributes :- NEVER thread-safe

Regards
Khilan Singh Raghuwanshi
Java Developer


Thanks & Regards
Khilan Singh
The Following User Says Thank You to khilansingh For This Useful Post:
dipak2704 (September 3rd, 2009)
 
Old November 11th, 2004, 08:22 AM
Registered User
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i agree with Khilan. it is good answer.

Regards,
Infant Prabu
 
Old February 22nd, 2007, 05:03 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I also agreed with Khilan Singh .

Can i get some more clarifications regarding to avoid the performance problem, a servlet container may create multiple instances of the servlet class.

For(a servlet container may create multiple instances) that, anything we want do in programmatically?

 
Old September 3rd, 2009, 02:35 PM
Registered User
 
Join Date: Sep 2009
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by khilansingh View Post
There is no such interface as MultiThreadedModel. Servlets are intrinsically multithreaded. This means a single instance can be accessed by more than one thread.

If the container receives multiple requests for one servlet simultaneously, the service () method of that servlet will be executed concurrently in multiple threads

If a servlet implements the SingleThreadModel interface, the container will not execute the service () method in more than one thread simultaneously.
The servlet container may synchronize access to a single instance of the servlet.
However, servicing requests sequentially seriously hurts performance. To avoid the performance problem, a servlet container may create multiple instances of the servlet class.



Different variables in both models thread safe or not:-
1. Single Thread Model:-
Local variables:- are defined within the body of a method. [Levi] Always thread-safe.

Instance variables:- Instance variables are defined within the class body and are separately assigned to each instantiated object. Thread-safe only for SingleThreadModel

Class or static variables:- Only one copy of a class variable exists across all the instances of the object belonging to the class for which it is declared.
Class variables , or static variables, are shared among all instances of a servlet. NEVER thread-safe
(No, not even if servlet implements SingleThreadModel.)

Request attributes :- Always thread-safe
Session attributes:- NEVER thread-safe
Context attributes :- NEVER thread-safe

Regards
Khilan Singh Raghuwanshi
Java Developer


Thanks & Regards
Khilan Singh
Hi Thilan

Its really good way to put info about thread safe variables/attributes.

Best Regards

Dipak S





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make servlet single threaded? harshala Apache Tomcat 0 June 20th, 2007 12:28 AM
how to make servlet single threaded? harshala Apache Tomcat 0 June 19th, 2007 08:51 AM
how can i implement thread in servlet?. spmanivel Servlets 3 September 14th, 2006 08:25 AM
how can i create Single Thread in servlet spmanivel Servlets 1 July 25th, 2006 06:14 PM
Multi thread objects sireesha VB How-To 1 May 27th, 2005 03:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.