我正在用Java构建一个带有Selenium和MVC设计模式的Web爬虫。我正在努力实现三件事:
以下代码解决了第一个问题,在某种程度上解决了第二个问题。我不得不制作一个令人讨厌的Thread.sleep()来防止它过早地在桌面上运行刷新。
public void runSearchItems() {
for (int i = 0; i < sidb.getSize(); i++) {
final int num = i;
Thread tn = new Thread(new Runnable() {
@Override
public void run() {
if (sidb.getSearhItem().get(num).getFormevent().getDomainBox().equalsIgnoreCase("www.someURL.com") == true) {
String searchField = sidb.getSearhItem().get(num).getFormevent().getSearchField();
int searchCat = sidb.getSearhItem().get(num).getFormevent().getSearchCategory();
boolean defect = sidb.getSearhItem().get(num).getFormevent().isDefectCheck();
boolean region = sidb.getSearhItem().get(num).getFormevent().isRegionCheck();
String arrange = sidb.getSearhItem().get(num).getFormevent().getArrangeBy();
ArrayList<SiteData> ls = wb.searchWebSite(searchField, searchCat, defect, region, arrange);
for (int j = 0; j < ls.size(); j++) {
db.addSiteData(ls.get(j));
}
}
}
});
tn.start();
try {
tn.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这样做的正确方法是什么?
谢谢!
对代码的细微更改
public void runSearchItems() {
List<Thread> allThreads = new ArrayList<>();
for (int i = 0; i < sidb.getSize(); i++) {
final int num = i;
Thread tn = new Thread(new Runnable() {
@Override
public void run() {
if (sidb.getSearhItem().get(num).getFormevent().getDomainBox().equalsIgnoreCase("www.someURL.com") == true) {
String searchField = sidb.getSearhItem().get(num).getFormevent().getSearchField();
int searchCat = sidb.getSearhItem().get(num).getFormevent().getSearchCategory();
boolean defect = sidb.getSearhItem().get(num).getFormevent().isDefectCheck();
boolean region = sidb.getSearhItem().get(num).getFormevent().isRegionCheck();
String arrange = sidb.getSearhItem().get(num).getFormevent().getArrangeBy();
ArrayList<SiteData> ls = wb.searchWebSite(searchField, searchCat, defect, region, arrange);
for (int j = 0; j < ls.size(); j++) {
db.addSiteData(ls.get(j));
}
}
}
});
allThreads.add(tn);
}
for(Thread t:allThreads)
t.start();
for(Thread t:allThreads)
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但是,你最好使用Thread Pool
以https://howtodoinjava.com/java/multi-threading/java-thread-pool-executor-example/为例