This is a program that reads information site for previous format it uses recursion and executor.It works fine,my problem is to test whether the program is completed and success notification.
public class NewClass {
static String levels[] = { "div.col-md-9 li a", "div#sidebar ul li a" };
static String links = "";
private void getRecursive(String href, int level, final ExecutorService executor) {
if (level > levels.length - 1) {
return;
}
Document doc;
try {
doc = Jsoup.connect(href).get();
Elements elements = doc.select(levels[level]);
final int flevel = ++level;
for (final Element element : elements) {
executor.execute(new Runnable() {
@Override
public void run() {
if (!element.attr("href").isEmpty()) {
links += element.attr("abs:href") + "\n";
System.out.println(links);
getRecursive(element.attr("abs:href"), flevel, executor);
}
}
});
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
If levels.length = 1, the rule enforcer works well,but If levels.length>1 will appear error : Exception in thread "pool-1-thread-138" java.util.concurrent.RejectedExecutionException
public static void main(String[] args) {
try {
ExecutorService executor = Executors.newFixedThreadPool(5);
new NewClass().getRecursive("http://www.java2s.com/", 0, executor);
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
if (executor.isTerminated()) {
JOptionPane.showMessageDialog(null, "Success");
}
} catch (Exception ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
解决方案
You can't submit any new tasks after you shutdown the ExecutorService, the recursion seems to stop after you've processed all the levels (you don't submit any new tasks after that), you can do something like this:
if (level > levels.length - 1) {
executor.shutdown();
return;
}