Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 3rd, 2003, 12:29 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old September 7th, 2003, 12:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
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
Reply With Quote
  #3 (permalink)  
Old September 10th, 2003, 01:43 AM
Authorized User
 
Join Date: May 2003
Location: New Delhi, Delhi, India.
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
Reply With Quote
  #4 (permalink)  
Old November 10th, 2004, 12:39 AM
Registered User
 
Join Date: Nov 2004
Location: Bangalore, Karnataka, India.
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
Reply With Quote
The Following User Says Thank You to khilansingh For This Useful Post:
dipak2704 (September 3rd, 2009)
  #5 (permalink)  
Old November 11th, 2004, 08:22 AM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i agree with Khilan. it is good answer.

Regards,
Infant Prabu
Reply With Quote
  #6 (permalink)  
Old February 22nd, 2007, 05:03 AM
Registered User
 
Join Date: Feb 2007
Location: , , .
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?

Reply With Quote
  #7 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.