GeeCON Prague 2014*
The moose likes Servlets and the fly likes Servlet init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet init()" Watch "Servlet init()" New topic
Author

Servlet init()

emmanuel ramos
Greenhorn

Joined: Dec 21, 2011
Posts: 13
hello, I have a question I'm reading a book servlets, but I do not understand something,
the book says that each request runs in a separate thread
but if I sobreescribo the method init () and print the name of the thread that runs, the same name appears in the JSP


//in the Servlet


public void init(){
System.out.println("Mi thread is "+ Thread.CurrenThread().getname());

}



//in the JSP


<%

System.out.println("Mi thread is "+ Thread.CurrenThread().getname());


%>


Why when I run for the first time have the same name


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Did you interpret that to mean that a new thread is created for every single request?

Because that isn't what actually happens in real-life servlet containers. There is a pool of threads which are reused as required. So a request which happens at 10 am and another request which happens 6 hours later might run in the same thread. All that can be said for sure is that two requests which are running at the same instant will be running in different threads.
emmanuel ramos
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Paul Clapham wrote:Did you interpret that to mean that a new thread is created for every single request?

Because that isn't what actually happens in real-life servlet containers. There is a pool of threads which are reused as required. So a request which happens at 10 am and another request which happens 6 hours later might run in the same thread. All that can be said for sure is that two requests which are running at the same instant will be running in different threads.





Just one question, which thread invokes the method init () and what happens with the thread
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Probably one of the threads in that thread pool. Why do you ask?
emmanuel ramos
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Paul Clapham wrote:Probably one of the threads in that thread pool. Why do you ask?



I wanted to know who invoked the method init (), but I see it is of the threads in the pool
thank you very much it was the question
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, you know, I've been writing and running servlets for years now, and I haven't ever cared how the container assigns servlets to threads. That information makes absolutely no difference to the way I write servlets. That's why I asked why you were interested in this topic.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet init()