【发布时间】:2017-08-21 21:45:40
【问题描述】:
在 htmlunit 中有没有办法用 TimeOut 以外的线程取消 getPage?
我试过Thread.stop,但是这个方法不方便。
【问题讨论】:
在 htmlunit 中有没有办法用 TimeOut 以外的线程取消 getPage?
我试过Thread.stop,但是这个方法不方便。
【问题讨论】:
我已经尝试并为我工作的是,在调用 getPage() 方法(从 WebClient 对象)之前,创建一个线程,在其构造函数中传递您将用于 getPage 的相同 webClient 对象。然后启动线程,然后调用 getPage() 方法。在线程内部,您应该调用 webClient.close(),它会中止 getPage 方法,但您将不得不处理 getPage() 方法的一些异常。
主线程代码:
WebClient webClient;
/* Initialize and configure webClient */
/* ... */
YourThread yourThread = new YourThread(webClient);
yourThread.start();
try {
webClient.getPage(someRequest);
} catch (IllegalStateException e) {
/* Ignore, it is caused because of aborting getPage */
}
你的线程代码:
private WebClient webClient;
public YourThread(WebClient webClient) {
this.webClient = webClient;
}
@Override
public void run() {
webClient.close();
}
【讨论】: