aspose file tools*
The moose likes Servlets and the fly likes Servlet run as a thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet run as a thread?" Watch "Servlet run as a thread?" New topic
Author

Servlet run as a thread?

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 504

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.


Millions saw the apple fall, but Newton asked why.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Not sure what you are asking. however, only one instance of the servlet class is created. For each request the container spawns another thread.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Sep 11, 2007
Posts: 504

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
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet run as a thread?