在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行,那么它应该立即处理所有。如何实现这一目标?任何一个例子都可以帮助我。
答案 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();
}
一些注意事项:
编辑 - 这是一个不同的命令,你的数据库连接早先发布:
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(...) {
...
}