BlockingQueue 多线程处理

更新表1000行 中的状态, 每次update 需要一秒的时间 模拟真实的业务逻辑。
怎么能最快的更新完表中所有的数据

SingleUpate 每次查询后更新
MultipleUpdate 用BlockingQueue 的方式实现多线程更新

SingleUpate

package com.tristan.bigdata;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SingleUpate {

public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "tristan", "654321");

PreparedStatement stmt = con.prepareStatement("update t_bigtable set status=? where id =?");


PreparedStatement pStmt = con.prepareStatement("select * from t_bigtable order by id");
ResultSet result = pStmt.executeQuery();
while (result.next()) {
int id = result.getInt(1);
System.out.println(id);
updateStatus(id, stmt, 2);
}

con.close();
}

private static void updateStatus(int id, PreparedStatement stmt, int status) throws SQLException, InterruptedException {

Thread.sleep(1000);
stmt.setInt(1, status);
stmt.setInt(2, id);

stmt.executeUpdate();
}
}



MultipleUpdate

package com.tristan.bigdata;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MultipleUpdate {

private static int maxStoreSize = 1000;
public static ArrayBlockingQueue queue = new ArrayBlockingQueue(
maxStoreSize);

public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "tristan", "654321");

PreparedStatement stmt = con
.prepareStatement("update t_bigtable set status=? where id =?");

PreparedStatement pStmt = con
.prepareStatement("select * from t_bigtable order by id");
ResultSet result = pStmt.executeQuery();
while (result.next()) {
int id = result.getInt(1);
System.out.println(id);

queue.put(id);
}

ExecutorService es = Executors.newCachedThreadPool();
Worker w = new Worker(stmt,3);

es.submit(w);
es.submit(w);
es.submit(w);
es.submit(w);
es.submit(w);
es.submit(w);
es.submit(w);
es.submit(w);
es.submit(w);
es.submit(w);

Thread.sleep(Long.MAX_VALUE);
// con.close();
}

}

class Worker implements Runnable {
PreparedStatement stmt;
int status;

public Worker(PreparedStatement stmt, int status) {
this.stmt = stmt;
this.status = status;
}

@Override
public void run() {
while(true){
try {
int id = (Integer)MultipleUpdate.queue.take();
System.out.println(Thread.currentThread().getName() + " : "+id);

Thread.sleep(1000);
stmt.setInt(1, status);
stmt.setInt(2, id);
stmt.executeUpdate();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}

}




SQL

select * from t_bigtable order by id;
truncate table t_bigtable;

declare
i number;
begin
i:=0;
for i in 1..1000 loop
insert into t_bigtable values(i,1);
end loop;
end;

你可能感兴趣的:(多线程,JVM,java,数据库)