For an object to be runnable, it needs to implement the Runnable
interface or extend the Thread
class, however, it does not seem that HttpServlet
does any of these.
How come HttpServlet
can be threaded or have i mistaken?
The Servlet itself is not a thread. The container maintains one instance of the servlet class and each request (thread) calls the same servlet object. So the servlet instances is shared across threads. In pseudo code it may look like this:
class ServerThread extends Thread {
private javax.servlet.Servlet servlet;
private javax.servlet.ServletRequest req;
private javax.servlet.ServletResponse res;
public ServerThread(javax.servlet.Servlet servlet, /* request and response */) {
this.servlet = servlet;
this.req = req;
this.res = res;
}
@Override
public void run() {
this.servlet.service(req, resp);
}
}
No question, in reality it will be much, much, much more complex :-)
BTW: That's the reason your servlet classes have to be thread safe!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With