我想等待每個請求10秒的請求,但請求不必等待對方。所以第二個請求不必等待20秒。Servlet中的異步進程
我的servlet:
@WebServlet(value = "/account", asyncSupported = true)
public class AccountServlet extends javax.servlet.http.HttpServlet {
public AccountServlet() {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
AsyncContext ac = request.startAsync();
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
executor.execute(new MyAsyncService(ac));
}
@Override
public void init(ServletConfig config) throws ServletException {
}
}
我的異步過程類:
class MyAsyncService implements Runnable {
AsyncContext ac;
public MyAsyncService(AsyncContext ac) {
this.ac = ac;
}
@Override
public void run() {
try {
System.out.println("started");
Thread.sleep(10000);
System.out.println("completed");
ac.complete();
} catch (InterruptedException ex) {
Logger.getLogger(MyAsyncService.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
爲什麼投票能解釋一下嗎? – Sarkhan