background image

 

对象,就不做处理。

sendRequestSynch 是同步的 API  

。 sendRequest()会调用同步方法,因此放在一个单

独 的 线 程 中 运 行 。 匿 名 线 程 t 启 动 之 后 , 通 过 request.wait() 方 法 , 使 调 用 
sendRequestSynch() 的 方 法 被 阻 塞 。 从 request.wait() 方 法 恢 复 之 后 , 判 断 是 否
timeout

 

发生。

public   void   sendRequestSynch(final   NetworkRequest   request,   long   timeout) 
throws Exception {
synchronized (request) {
Thread t = new Thread() {
public void run() {
try {
log.debug("send request in another thread.");
sendRequest(request);
}catch (Exception e) {
log.warn(e.getMessage());
}
log.debug("Exit the thread.");
}
};
t.start();
log.debug("Try to wait for " + timeout);
request.wait(timeout);
if (request.isDelayed()) {
queue.removeElement(request);
timeoutQueue.addElement(request);
log.debug("Request TIMEOUT !!!!!!!!!!!!!!!!");
log.debug("Stop the network request thread for the timeout.");
}else {
log.debug("Be Notified and wakeup.");
}
}
}

sendRequestAsynch 是异步的 API。这个是写作过程中的一个版本。在 review 之后发现
其中存在同步问题。synchronized (request)不是在启动孙子线程 tt 之后执行。问题在于
如果 sendRequest(request)在执行到最后是发送 request.notifyAll()时,可能还没有
执行 synchronized (request) 。这样就存在 request.wait(timeout)不会被唤醒而超时

 

的问题。
public   void   sendRequestAsynch(final   NetworkRequest   request,   final   long 
timeout) throws Exception {