HttpServlet 如何在不实现 Runnable 或扩展线程的情况下创建线程
众所周知,当servlet收到请求时,它会创建一个新线程,并在新线程内调用service方法。因此,只有一个 Servlet 实例,就会调用许多线程。
我不明白的是 HttpServlet 如何能够在不实现可运行或扩展线程的情况下创建自己实例的线程?
任何人都可以澄清一下吗?
As we know, when servlet receivies a request, it creates a new thread and inside the new thread, the service method is invoked. So with only one Servlet instance, many threads are invoked.
What I didn't understand is how HttpServlet
is able to create threads of its own instance without implementing runnable or extending thread?
Can any one please clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,这是不正确的。 Web 容器通常维护一个有界的工作线程池来处理请求。不会为每个新请求创建新线程。
基本上,事实并非如此。
HttpServlet
不会创建线程,它不是线程或可运行对象。Web 容器具有实现 Runnable 或扩展 Thread 的特定实现类。这些类调用(共享)Servlet 实例上的相关方法。
In fact, that is INCORRECT. The web container typically maintains a bounded pool of worker threads to handle requests. New threads don't get created for each new request.
Basically, it doesn't.
HttpServlet
does not create threads, and it is not a thread or a runnable.The web container has implementation specific classes that implement
Runnable
or extendsThread
. These classes call the relevant methods on the (shared)Servlet
instance.这是由 Servlet 容器(也称为 Web 容器)处理的。
Servlet容器负责维护Servlet生命周期。
http://en.wikipedia.org/wiki/Java_Servlet
This is handled by the Servlet Container (also called the Web Container).
The Servlet Container is responsible for maintaining the Servlet Lifecycle.
http://en.wikipedia.org/wiki/Java_Servlet