• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet run as a thread?

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read SL-314 by Sun Micro system,In that

The servlet runs as a thread in web container instead of in separate OS process.


When the no of requests for a servlet rises, no additional instances of the servlet or OS processes are created. Each request is processed concurrently using one Java thread per request.



By reading this i get question in my mind
1.Is servlet itself is one thread or it act like thread for some times?
2.when servlet run in memory that time this thread concept is by OS therad concept(System programming) or our java Thread concept( which in core java)?
3.The request convert into threads by using method thread in servlet.java as follows:
<blockquote>code:
<pre name="code" class="core"> public Thread(String name) {
init(null, null, name, 0);}
public Thread(ThreadGroup group, String name){
init(group, null, name, 0);}
public Thread(Runnable target, String name) {
init(null, target, name, 0);}
public Thread(ThreadGroup group, Runnable target, String name) {
init(group, target, name, 0);}
public Thread(ThreadGroup group, Runnable target, String name,
long stackSize) {
init(group, target, name, stackSize);}
</pre>
</blockquote>
This code i read in servlet.java.
Please help me, i am confuse in Thread concept using in servlet or Servlet run as thread.
Thanks in advance.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you are asking. however, only one instance of the servlet class is created. For each request the container spawns another thread.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet container takes care of creating a Thread pool and assigning Threads to requests for you. All you have to worry about is making sure your code is "thread-safe."

Bill
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying,
Sorry for writing

3.The request convert into threads by using method thread in servlet.java as follows:

code:
public Thread(String name) {
init(null, null, name, 0);}
public Thread(ThreadGroup group, String name){
init(group, null, name, 0);}
public Thread(Runnable target, String name) {
init(null, target, name, 0);}
public Thread(ThreadGroup group, Runnable target, String name) {
init(group, target, name, 0);}
public Thread(ThreadGroup group, Runnable target, String name,
long stackSize) {
init(group, target, name, stackSize);}
This code i read in servlet.java.

,
it is Thread.java not servlet.java
Now i want to ask, when we write servlet program, as every java program by default import java.lang.*; then when

The servlet runs as a thread in web container instead of in separate OS process. or hen the no of requests for a servlet rises, no additional instances of the servlet or OS processes are created. Each request is processed concurrently using one Java thread per request.

Then this thread.java which is 1 of in java.lang.*,calls or not?
As William Brogden write

All you have to worry about is making sure your code is "thread-safe."

What is meaning of "thread-safe"?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The subject of Thread safety has come up in this forum many times. You might try browsing the last few weeks of posts for more thoughts.

Thread-safe means that your program is designed so that the data for each separate request cannot be modified by other requests. For example, since there is only one instance of a servlet object handling any number of requests, you must not use instance variables to hold request specific variables. Any good servlet tutorial will cover this.

Bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic