public class ExampleServlet extends HttpServlet{
private Closure closure;
@Override
protected void doPost(final HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Do some stuff here...
if (this.closure == null){
this.closure = new Closure(){
@Override
public void someFunction(){
req.getRequestURI(); // Obviously do something with it...
}
}
}
// Later...
this.closure.someFunction(); // << Is this thread-save??
// More stuff here...
}
}
在這個例子中,我有過Closure
管不着!這是否緩存關閉線程保存?
如果我測試這個,它工作正常。現在的問題是:
是否每個請求的線程得到closure
已場內的一個新副本?否則,當有新的請求進入時,引用的req
字段將會改變,而someFunction()
仍在執行。或者這是通過宣佈req
爲final
來處理的?
的'如果(this.closure == NULL){..},因爲它需要同步'的部分是不是安全:) – zapl 2014-12-06 00:49:30