Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how thread are created by the container

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what i know...
Threads can be created by extending Thread class or by implementing Runnable.
In the first case, to set the thread in running state, we create a object of the class that extends the Thread class and call the start() which in turn calls the run().
In the second case, We create a thread object and give the thread the runnable class object. And we call the start() using the thread reference.

HFSJ wrote:The container finds the correct servlet based on the URL in the request, creates or allocates a thread for that request and calls the servlet's service() ,passing the req & res object as arguments

So how do the container create the thread,
If its the first case. then the servlet that i write does not extend thread class, then how is it done?
If it is the second case , then also my servlet is not implementing the runnable??

Could you explain how this is done...
Or am i missing something very badly???

I mean to find out .... what is the argument that thread is getting ??? a servlet object? or ....???
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose this is the container's code to start a new thread


When the servler receives a request, it will just create an instance of this class (i.e. ProcessRequest) with the servlet to be called and the request and response objects and then call start on the instance of this class. Now your servlet class doesn't need to implement Runnable or extend Thread. I don't know exactly what technique the container uses to call the service method, but it must be similar to the one above...
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:When the servler receives a request, ..

I think you meant container...
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sony agrwal wrote:
Ankit Garg wrote:When the servler receives a request, ..

I think you meant container...

Yup
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic