File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet init()

 
emmanuel ramos
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably one of the threads in that thread pool. Why do you ask?
 
emmanuel ramos
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic