ExecutorService未接来电

时间:2019-04-27 13:54:04

标签: java multithreading

我的ExecutorService出现问题,它无法执行所有调用。

public class MainClass {

    public static void main(String[] args) throws IOException, InvalidConnection, InterruptedException {
        ExecutorService executor = Executors.newCachedThreadPool();

        XMLUrlService xmlUrlService = new XMLUrlService();
        LinkedBlockingQueue<String> linkedBlockingQueue = xmlUrlService.getAllXMLUrls();

        System.out.println(linkedBlockingQueue.size());
        for (int i = 0; i < linkedBlockingQueue.size(); i++) {

            executor.execute(new XMLParser(linkedBlockingQueue));
        }

        executor.shutdown();
    }

}

XMLUrlService类返回一个URL列表,然后它执行连接。

XMLPaser是负责与给定URL建立连接的类。


public class XMLParser implements Runnable {


    private LinkedBlockingQueue queue;

    public XMLParser(LinkedBlockingQueue queue) {
        this.queue = queue;
    }

    public XMLParser(){}

    public void getRates(String data) throws IOException, XMLStreamException, InvalidConnection {

            URL url = new URL(data);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            InputStream inputStream = connection.getInputStream();

            inputStream.close();
            connection.disconnect();
    }


    public void run() {
            try {
                String data = (String) queue.take();
                getRates(data);

            } catch (InterruptedException | IOException ex) {
                //Handle exception
            } catch (InvalidConnection invalidConnection) {
                invalidConnection.printStackTrace();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
    }
}

我的阻止队列包含250个不同的URL地址,但是executorservice执行随机数量的调用(250仅是要测试的随机元素数量)。我认为当我从队列中取出物品时,可以省略一些物品,但是我不确定。

1 个答案:

答案 0 :(得分:0)

您应该更改三件事:

执行任务的方式
您以一种非常奇怪的方式执行任务。像这样传递linkedBlockingQueue而不是传递String

System.out.println(linkedBlockingQueue.size());
for (int i = 0; i < linkedBlockingQueue.size(); i++) {
    String url = linkedBlockingQueue.remove()
    executor.execute(new XMLParser(url));
}

处理异常的方式
不要吞下异常-最简单的方法是使用Callable来获取Future,以便以后可以检查任务发生了什么:

    ExecutorService executor = Executors.newCachedThreadPool();

    LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<String>();

    System.out.println(linkedBlockingQueue.size());

    List<Future<Void>> result = linkedBlockingQueue.stream().map(
            url -> executor.submit(new XMLParser(url))
    ).collect(Collectors.toList());

    for (Future<Void> future : result) {
        try {
            future.get();
        } catch (Exception e) {
            System.out.println("Something happened:" + e);
        }
    }

    executor.shutdown();

等待执行 关闭执行程序后,请等待所有任务完成。

executor.shutdown();
executor.awaitTermination(10, TimeUnit.HOURS);