简体   繁体   中英

Is there a way class that extends httpservlet and thread

I need an answer for the question i have mentioned in the title.

No. A Java class can only have one superclass.

Why would you want to though? You can extend HttpServlet and implement Runnable. You can then call new Thread(this) - although frankly I'd recommend separating out the servlet and the runnable aspect anyway.

No, there is no way to extends from two classes in Java. If you want you can have your servlet class implement java.lang.Runnable interface. But if you explained us better what yi are actually trying to achieve we could help you better.

i have servlet and thread. In thread i am trying to run the batch file and wait for 15 minutes.

i have something in servlet to load the image in html page after waiting for 15 miutes

Look at javax.servlet.ServletContextListener for implementing threads in a web application. It is generally a bad idea to start threads in servlets - the listener makes sure you only have one thread and that it is started when the application starts. It also allows you to elegantly stop the thread when the application is stopped (ie when the servlet context is destoyed). You can use a Thread Monitor pattern to send a notification to the thread to start/pause/stop processing etc. methods in this pattern would be Thread.notify() and Thread.wait(). You can create the thread in the contextInitialized() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM