并行运行while(resultset.next())

时间:2016-12-10 10:33:45

标签: java multithreading parallel-processing

在java 7中。

我有一个查询。

car

//假设行的值为1或2或3,则

String sql = "select path, job_id from job_table where status = 'NEW' and rownum <= 5 order by created_date"; 


 ResultSet rows = null; 
 try 
 {  
      conn = getConnection();
      preparedStatement = conn.preparedStatement(sql);
     rows  =preparedStatement.executeQuery();

      while(rows.next())
     {  

  // do stuff or call respective methods depending on the values received from resultset.

现在我要做的事情是平行的。以前,当我运行我的应用程序时,它实际上完成了每行的其他功能,然后处理下一行。但如果我有3到4行,那么它应该立即处理所有。如何实现这一目标?任何一个例子都可以帮助我。

1 个答案:

答案 0 :(得分:1)

编辑Java 7:

private static final int NUM_THREADS = 8; // Can get from runtime to scale to number of cores
private static final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);

String sql = "select path, job_id from job_table where status = 'NEW' and rownum <= 5 order by created_date"; 

ResultSet rows = null; 
try 
{  
    conn = getConnection();
    preparedStatement = conn.preparedStatement(sql);
    rows  =preparedStatement.executeQuery();

    List<Future<?>> futures = new ArrayList<>();
    while(rows.next())
    {
        // Getting values from ResultSet here, to not assume thread-safety of ResultSet
        final String path = rows.getString("path");
        final int job_id = rows.getInt("job_id");
        futures.add(executor.submit(new Runnable() {
            @Override
            public void run() {
                // do stuff or call respective methods depending on the values received from resultset.
            }
        });
    }
    // Wait until all finished
    for(Future<?> future : futures) future.get();
}

一些注意事项:

  • 您可能需要终止执行服务以进行关闭
  • 如果您需要从“do stuff”
  • 获取结果,可以使用Callable接口
  • 您的try / catch中还有一些例外情况需要处理。
  • 你的情况下NUM_THREADS可能是5?
  • run()中的任何已检查异常都可以包装在RuntimeException中,然后重新抛出。使用Callable不需要这个。

编辑 - 这是一个不同的命令,你的数据库连接早先发布:

private static final int NUM_THREADS = 8; // Can get from runtime to scale to number of cores
private static final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);

String sql = "select path, job_id from job_table where status = 'NEW' and rownum <= 5 order by created_date"; 

try {
    List<Future<?>> futures = new ArrayList<>();
    try (
        Connection conn = getConnection();
        PreparedStatement preparedStatement = conn.preparedStatement(sql);
        ResultSet rows = preparedStatement.executeQuery()
    ) {
        while(rows.next())
        {
            // Getting values from ResultSet here, to not assume thread-safety of ResultSet
            final String path = rows.getString("path");
            final int job_id = rows.getInt("job_id");
            futures.add(executor.submit(new Runnable() {
                @Override
                public void run() {
                    // do stuff or call respective methods depending on the values received from resultset.
                }
            });
        }
    }
    // Wait until all finished, database is already release while background processing is still ongoing
    for(Future<?> future : futures) future.get();
} catch(...) {
    ...
}