在htmlunit中有没有办法用TimeOut以外的线程取消getPage?
我尝试过Thread.stop,但这种方法不方便。
答案 0 :(得分:0)
在我调用getPage()方法(来自WebClient对象)之前,我尝试过并且为我工作过,创建一个线程,在其构造函数中传递与getPage相同的webClient对象。然后启动线程,然后调用getPage()方法。在线程内部你应该调用webClient.close()并且它将中止getPage方法,但是你将不得不在getPage()方法上处理一些异常。
MainThread代码:
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 */
}
YourThread代码:
private WebClient webClient;
public YourThread(WebClient webClient) {
this.webClient = webClient;
}
@Override
public void run() {
webClient.close();
}