public boolean service(Request request,
Response response,
RequestHandler handler) throws IOException {
final Holder< IOException > exceptionHolder = new Holder< IOException >();
Invokable< Boolean > invokable = new Invokable< Boolean >()
{
public Boolean invoke()
{
if (System.currentTimeMillis() - lastCheck >= checkInterval)
barrier.tryWithWrite(checker, updateTimeout, TimeUnit.MILLISECONDS);
// And, now, back to code within the read lock.
try
{
return handler.service(request, response);
}
catch (IOException ex)
{
exceptionHolder.put(ex);
return false;
}
}
};
// Obtain a read lock while handling the request. This will not impair parallel operations, except when a file check
// is needed (the exclusive write lock will block threads attempting to get a read lock).
boolean result = barrier.withRead(invokable);
if (exceptionHolder.hasValue()) throw exceptionHolder.get();
return result;
}
|