2

我想以给定的格式将 ExecutorService 应用于来自 url 的递归读取路径,我很难尝试调用递归 getRecursive(element.attr("abs:href"), level);

public class NewClass {

    static String levels[] = {"div.col-md-9 li a", "div#sidebar ul li a"};

    private void getRecursive(String href,int level) {

        if (level > levels.length - 1) {
            return;
        }

        Document doc;
        try {
            doc = Jsoup.connect(href).get();
            Elements elements = doc.select(levels[level]);

            level++;
            ExecutorService executor = Executors.newFixedThreadPool(5);
            for (final Element element : elements) {
                executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        if (!element.attr("href").isEmpty()) {
                            String links = "";
                            links += element.attr("abs:href") + "\n";
                            System.out.println(links);
                            getRecursive(element.attr("abs:href"), level);
                        }
                    }
                });
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new NewClass().getRecursive("http://www.java2s.com/", 0);
    }
}
4

1 回答 1

4

level变量必须是final为了在run()方法中访问它。无论如何,如果您没有ExecutorService正确使用,您的程序不仅限于5线程,因为您ExecutorService每次调用该getRecursive(...)方法时都会创建一个新的。

要对每个递归使用相同ExecutorService的,您可以执行以下操作:

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();   
        }   
    }   

    public static void main(String[] args) {    
        ExecutorService executor = Executors.newFixedThreadPool(5); 
        new NewClass().getRecursive("http://www.java2s.com/", 0, executor); 
    }   
}
于 2015-11-02T03:39:14.103 回答