import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
/**
* Created by DowneyJr on 2017/1/12.
* 1.这里比较一次性的插入和多线程插入,所以要用CountDownLatch进行加锁
* 2.参考网址:http://blog.csdn.net/javastart/article/details/49903437
* 3.可以优化成数据库的连接池就更加快速的批量操作数据了
*/
public class ThreadImport {
private String url="jdbc:oracle:thin:@127.0.0.1:1521:XE";
private String user="scott";
private String password="tiger";
public Connection getConnect(){
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con= DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public void multiThreadImport( final int ThreadNum){
final CountDownLatch cdl= new CountDownLatch(ThreadNum);
long starttime=System.currentTimeMillis();
for(int k=1;k<=ThreadNum;k++){
new Thread(new Runnable() {
@Override
public void run() {
Connection con=getConnect();
try {
Statement st=con.createStatement();
for(int i=1;i<=80000/ThreadNum;i++){
String uuid= UUID.randomUUID().toString();
st.addBatch("insert into demo_table(a,b) values('"+uuid+"','"+uuid+"')");
if(i%500==0){
st.executeBatch();
}
}
cdl.countDown();
} catch (Exception e) {
}finally{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}).start();
}
try {
cdl.await();
long spendtime=System.currentTimeMillis()-starttime;
System.out.println( ThreadNum+"个线程花费时间:"+spendtime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
ThreadImport ti=new ThreadImport();
ti.multiThreadImport(1);
ti.multiThreadImport(5);
ti.multiThreadImport(8);
ti.multiThreadImport(10);
ti.multiThreadImport(20);
ti.multiThreadImport(40);
System.out.println("笔记本CPU数:"+Runtime.getRuntime().availableProcessors());
}
}