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<Integer> queue = new ArrayBlockingQueue<Integer>(
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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值