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...
}
}
In the example, I have no control over Closure
!
If I test this, it works fine. The question is:
Does every request-thread get a new copy of the closure
-field? Otherwise, when a new request comes in, the referenced req
-field would change while the someFunction()
is still executing. Or is this handled by declaring req
as final
?