我一直在研究一个项目。该程序的一部分是多线程代码,其中线程1在搜索引擎上搜索一个术语,线程2._0到2._7接收搜索结果并读取链接的内容,而线程3.创建单词包结果链接内容之外(此类的完整代码可以在https://github.com/ikb-a/ci/blob/summer2015/src/main/java/openEval/MultithreadSimpleOpenEval.java找到)。 在运行代码时,只要其中一个线程2出现在需要身份验证的网站上,我就会得到一个弹出窗口,要求提供登录凭据(参见下图)
这是我的程序的一个主要问题,因为弹出窗口会冻结线程,直到人按下取消按钮。
在互联网上搜索后,我发现了一个与eclipse有关的类似问题,所以我将项目导出为runnable .jar并重新运行它,期望消息不显示。但它确实如此(见下文):
然后我查看了我的程序正在创建的日志消息,并发现线程2._1打印了以下消息:
2._1阅读:philippineexaminer.com/CSS 国外有组织的菲律宾组织被要求帮助街头儿童...... http://www.philippineexaminer.com/CSS/
所有其他线程都已完成,但线程3正在等待2._1。按下弹出窗口上的取消按钮后,我得到了以下日志,这应该是没有出现弹出窗口的情况:
2._1无法读取http://www.philippineexaminer.com/CSS/原因:org.jsoup.HttpStatusException:HTTP错误提取URL。状态= 401,网址= http://www.philippineexaminer.com/CSS/
以下是程序冻结的代码提取:
try {
if (verbose)
System.out.println("2." + name + " Reading: " + link);
// read the contents of the website
// TODO: set jsoup so as to reject any website that
// request authentication
String websiteAsString = Jsoup.connect(link.getLink())
.userAgent(
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0")
.referrer("http://www.google.com").get().text();
if (!websiteAsString.isEmpty()) {
if (verbose)
System.out.println("2." + name + " Adding result to contents");
contents.add(websiteAsString);
if (memoizeLinkContents) {
memoizedLinkContents.put(link.getLink(), websiteAsString);
}
}
} catch (Exception e) {
if (verbose)
System.err.println("2." + name + " Unable to read " + link.getLink() + " CAUSE: " + e);
if (memoizeLinkContents) {
memoizedLinkContents.put(link.getLink(), "");
}
}
从日志中我知道Jsoup调用就行了,但Jsoup无法创建弹出窗口,也无法执行javascript。另外,我知道调用link.getLink()不能负责,因为它是一个简单的信息检索(下面链接类“SearchResult”的代码):
import java.io.Serializable;
/**
* An object which holds one search result from an engine such as Google Search.
* This class is immutable.
*
*/
public class SearchResult implements Serializable {
/**
* serial version UID generated by eclipse
*/
private static final long serialVersionUID = -1239745645367856189L;
/**
* Title of this webpage according to the search engine.
*/
private final String title;
/**
* link to this webpage according to the search engine.
*/
private final String link;
/**
* Snippet of the text in this webpage according to the search engine.
*/
private final String snippet;
public SearchResult(String title, String link, String snippet) {
super();
this.title = title;
this.link = link;
this.snippet = snippet;
}
/**
* Returns the title of this result (the title of this webpage).
*/
public String getTitle() {
return title;
}
/**
* Returns the link to this webpage.
*/
public String getLink() {
return link;
}
/**
* Returns a snippet of text from the webpage (typically containing some or
* all of the words in the query that produced this result).
*/
public String getSnippet() {
return snippet;
}
@Override
public String toString() {
return String.format("%s\n%s\n%s\n", getTitle(), getSnippet(), getLink());
}
/**
* An object is equal to this if the object is also a {@link SearchResult},
* with the same values for {@link #getTitle()}, {@link #getLink()} and
* {@link #getSnippet()}.
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof SearchResult)) {
return false;
}
SearchResult o = (SearchResult) obj;
return this.getLink().equals(o.getLink()) && this.getTitle().equals(o.getTitle())
&& this.getSnippet().equals(o.getSnippet());
}
}
任何人都有任何想法可能会导致这种行为? 我正在运行Ubuntu 16.04 LTS 记忆:3.7 GiB 处理器:英特尔®奔腾(R)CPU 2127U @ 1.90GHz×2 图形:英特尔®IvybridgeMobile 操作系统类型:64位 磁盘:487.9 GB
有关课程的完整代码可在https://github.com/ikb-a/ci/blob/summer2015/src/main/java/openEval/MultithreadSimpleOpenEval.java
找到虽然此类不包含main方法,但它是使用链接http://www.philippineexaminer.com/CSS/的四个类之一。其他类是SearchResult类(如上所示)和2个搜索引擎类,它们不能负责,因为它会导致线程1.而不是线程2._1冻结。
我尝试使用以下代码单独重现弹出窗口:
package PlanIt.monthSuggestion.trainingDataGeneration;
import java.io.IOException;
import org.jsoup.Jsoup;
public class Exp5 {
public Exp5() {
}
public static void main(String[] args) throws IOException {
String websiteAsString = Jsoup.connect("http://www.philippineexaminer.com/CSS/")
.userAgent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0")
.referrer("http://www.google.com").get().text();
System.out.println(websiteAsString);
}
}
但是,我得到了所需的结果,而不是弹出窗口,而是一条401错误消息:
线程“main”中的异常org.jsoup.HttpStatusException:HTTP错误提取URL。状态= 401,网址= http://www.philippineexaminer.com/CSS/ at org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:537) at org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:493) 在org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205) 在org.jsoup.helper.HttpConnection.get(HttpConnection.java:194) 在PlanIt.monthSuggestion.trainingDataGeneration.Exp5.main(Exp5.java:15)