即使其线程正在运行,Java executorsevice仍在关闭

时间:2016-02-12 10:27:32

标签: java jdbc

我正在使用Executorsevice从数据库生成文件。我使用jdbc和核心java将表数据转换为文件。

在创建具有10个线程的Executorservice后,我在for循环中提交60个线程以并行获取60个文件。这适用于小数据和几列的表。但是对于一个巨大的文件和具有更多列的表,正在处理大型表数据/更多列表的线程在其他线程完成时停止而不在日志中提供任何信息。

ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
        for (String filename : filenames) {
            EachFileThread worker = new EachFileThread(destdir, converter,
                    filename, this);
            executor.execute(worker);
        }
        executor.shutdown();

Inside Eachfilethread我正在读取xml并获取列,表并形成查询并执行查询并格式化数据并将数据放入文件

    forTable = (FileData) converter.convertFromXMLToObject( filename + ".xml");
    String query = getQuery(forTable);
    statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    resultSet = statement.executeQuery(query);
resultSet.setFetchSize(3000);
    WriteData(resultSet, filepath, forTable);(formatting the data from db and then writing to a file)

1 个答案:

答案 0 :(得分:1)

问题是你不是在等所有工作完成他们正在做的事情。正如@msandiford在评论中建议的那样,您应该在调用awaitTermination(..)之后添加对shutdown()的调用,因为它位于https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

上的样本shutdownAndAwaitTermination()方法中

例如,您可以尝试这样做:

ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
for (String filename : filenames) {
    EachFileThread worker = new EachFileThread(destdir, converter, filename, this);
    executor.execute(worker);
}
executor.shutdown();
try {
    // Wait a while for existing tasks to terminate
    if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
        executor.shutdownNow(); // Cancel currently executing tasks
        // Wait a while for tasks to respond to being cancelled
        if (!executor.awaitTermination(60, TimeUnit.SECONDS))
            System.err.println("Executor did not terminate");
    }
} catch (InterruptedException ie) {
    // (Re-)Cancel if current thread also interrupted
    executor.shutdownNow();
    // Preserve interrupt status
    Thread.currentThread().interrupt();
}