我有一个java servlet并且正在使用jetty。收到POST请求后,我尝试向另一个服务发送另一个请求。收到其他服务的响应后,我想唤醒我的线程并继续发送响应。我正在尝试使用对象'lock'来等待并通知。任何人都可以告诉我为什么我的线程没有在lock.wait()上醒来?
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
Object lock = new Object();
client.newRequest("http://localhost:3699/create")
.method(HttpMethod.POST)
.content(new StringContentProvider(ServletUtils.extractRequestBody(request)))
.send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
ServletUtils.forwardResponse(response, result, getContentAsString());
System.out.println("NOTIFY");
synchronized (lock) {
lock.notifyAll();
}
System.out.println("DONE NOTIFICATION");
}
});
try {
System.out.println("WAIT");
lock.wait();
System.out.println("DONE WAITING");
} catch (InterruptedException e) {
ServletUtils.badRequest(response);
}
}
我的打印声明说
WAIT
WAIT
NOTIFY
DONE NOTIFICATION
a)我不确定为什么我会获得2次等待
b)我没有完成等待