• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet run as a thread?

 
Mandar Khire
Ranch Hand
Posts: 567
Android Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • 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: 567
Android Java Linux
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic