`javax.servlet.AsyncContext.startAsync()`和`new Thread(Runnable).start()`之间的区别是什么?

时间:2015-03-28 13:20:19

标签: java-ee-6 java-ee-7

我已阅读文章17.12 Asynchronous Processing

之间有什么区别:

public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    AsyncContext acontext = req.startAsync();
    ...
}

public void doGet(HttpServletRequest req, HttpServletResponse resp) {
   Runnable runnable = ...
   new Thread(runnable).start();
   ...
}

1 个答案:

答案 0 :(得分:1)

不同之处在于'谁'正在控制线程。当您从请求中获取AsyncContext时,使用其start方法并传递Runnable实例,容器管理线程,在该线程中执行您在Runnable实例中传递的逻辑。使用新线程(runnable).start()

触发线程时不是这种情况