【发布时间】:2015-10-30 05:28:37
【问题描述】:
我有这个递归函数,它可以在 URL 上找到 href 并将它们全部添加到全局列表中。这是同步完成的,需要很长时间。我试图用线程来做到这一点,但未能让所有线程写入一个列表。有人可以告诉我如何使用线程来做到这一点吗?
private static void buildList (String BaseURL, String base){
try{
Document doc = Jsoup.connect(BaseURL).get();
org.jsoup.select.Elements links = doc.select("a");
for(Element e: links){
//only if this website has no longer been visited
if(!urls.contains(e.attr("abs:href"))){
//eliminates pictures and pdfs
if(!e.attr("abs:href").contains(".jpg")){
if(!e.attr("abs:href").contains("#")){
if(!e.attr("abs:href").contains(".pdf")){
//makes sure it doesn't leave the website
if(e.attr("abs:href").contains(base)){
urls.add(e.attr("abs:href"));
System.out.println(e.attr("abs:href"));
//recursive call
buildList(e.attr("abs:href"),base);
}
}
}
}
}
}
} catch(IOException ex) {
}
//to print out all urls.
/*
* for(int i=0;i<urls.size();i++){
* System.out.println(urls.get(i));
* }
*/
}
【问题讨论】:
-
你的线程代码在哪里?
-
@redFIVE 我最初是作为可运行的。由于其他外部因素,Runnable 扩展不是一个可接受的解决方案。我没有其他方面的多线程经验,因此这个问题。
-
你有没有想过尝试谷歌线程间通信方法?
-
@redFIVE 如果您不想提供帮助,那很好,请不要打扰我。我一直在尝试各种事情。这是一个我知道它可以正常工作的状态,我希望从这里获得帮助。
-
@redFIVE 虽然我同意 OP 可能会做更多的研究,但没有理由变得刻薄和咄咄逼人。如果您不喜欢这个问题,只需投反对票,如果您认为有必要,请标记,然后继续。
标签: java multithreading recursion