Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Each Request in a new Thread ?

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a thread concept, the class must with implement Runnable or extend Thread.
Then thread's run() method is where the job is being done.

In servlet's we mention that "each request in a new Thread"? Which class is implement Runnable or extend Thread. Moreover is the run() method calls the service() method OR IN other words HOW Thread concept maps in servlet case.
 
Harshit Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my understanding, for each request, container creates a new thread; in that thread's run method, the corresponding servlet's service method is called, which in turn calls doGet/doPost method of the servlet
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but for creating a thread, the class should either implement runnable or extend Thread but our servlet class doesn't do it.?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the botheration of the container. I guess!

Any thoughts guys??
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes...
it very much internal to the conatiner, how to run each request in a seperate thread
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Vaid:
but for creating a thread, the class should either implement runnable or extend Thread but our servlet class doesn't do it.?


Good question. Technically, to create a Thread you need a java.lang.Thread object and run() method of that Thread is a place where you specify what you want to execute in that thread. You can specify anything you like, such as:



So in effect, the service() method of the servlet class will be excuted in a thread if you do new WorkerThread().start();

Runnable makes this isdirection a little easier because Thread class has inbuilt support for it. It does not need reflection because it knows about Runnable class at compile time. So all it does is:
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Paul's code is interesting - never ever ever ever ever ever ever ever ever ever ever *breath* ever ever ever ever do that in the real world!

Creating threads for requests and calling the service method is completely the job of the container. Never try to do the container's work. The container works under a Union that is backed by a rough mob. If you steal the container's job, the mob will hunt you down and they will tickle you until you die of laughter. (It's a lot uglier than it sounds.)

From a plain SCJP (or SCJD) perspective, I can understand the concern for "Where do baby threads come from?" But in web programming, you sometimes just have to trust that the container stork brings them and you just need to worry about handling them.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Peabody:
While Paul's code is interesting - never ever ever ever ever ever ever ever ever ever ever *breath* ever ever ever ever do that in the real world!



That's absolutely correct. I was just trying to show how a container *might* do it even though your servlet doesn't extend Thread or implement Runnable. So the code above is NOT of a servlet but a part of a servlet container
 
Jon Lee
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Peabody:
container works under a Union that is backed by a rough mob. If you steal the container's job, the mob will hunt you down and they will tickle you until you die of laughter. (It's a lot uglier than it sounds.)[/QB]




This answer is very funny...Good on ya, mate...
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Paul's code is interesting - never ever ever ever ever ever ever ever ever ever ever *breath* ever ever ever ever do that in the real world!

Creating threads for requests and calling the service method is completely the job of the container. Never try to do the container's work. The container works under a Union that is backed by a rough mob. If you steal the container's job, the mob will hunt you down and they will tickle you until you die of laughter. (It's a lot uglier than it sounds.)

From a plain SCJP (or SCJD) perspective, I can understand the concern for "Where do baby threads come from?" But in web programming, you sometimes just have to trust that the container stork brings them and you just need to
worry about handling them.


he he ! well said marc..
i guess..... pauls code was 'height of curiousity' as to how the container
does it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic