/**
D:/record.txt 文件是一行一行的文字,每行文字其实是一个十位数,文件中基本没重复的数字
我就是将其插入mysql 的record表中,record表只有一列id,定义为varchar(13) unique key
结果我几十个线程,每秒钟也插入一百左右,并且cpu空闲的很
请问是怎么回事啊,代码如下
*/import com.mysql.jdbc.*;
public class Try implements Runnable{
private static BufferedReader in ;
public static void main( String[] args ) {
for( int i=0;i<57;i++ ) {
new Thread(new Try()).start();
}
}
public Try() {
if( in != null ) return ;
try{
in = new BufferedReader( new FileReader("D:/record.txt") );
}catch( Exception e ){
e.printStackTrace();
}
}
public void run() {
java.sql.Connection con;
java.sql.Statement sta;
try{
Class.forName("com.mysql.jdbc.Driver" );
String url = "jdbc:mysql://localhost:3306/test";
con = java.sql.DriverManager.getConnection(url,"root","password");
String line;
int N=0;
while( (line=in.readLine())!=null ) {
try{
con.createStatement().execute("insert into record values('"+line.trim()+"')" );
N++;
}catch( Exception e ){
System.out.println( "N"+N +e.toString() );
}
}
System.out.println( N );
}catch( Exception e ){
e.printStackTrace();
}
}}
D:/record.txt 文件是一行一行的文字,每行文字其实是一个十位数,文件中基本没重复的数字
我就是将其插入mysql 的record表中,record表只有一列id,定义为varchar(13) unique key
结果我几十个线程,每秒钟也插入一百左右,并且cpu空闲的很
请问是怎么回事啊,代码如下
*/import com.mysql.jdbc.*;
public class Try implements Runnable{
private static BufferedReader in ;
public static void main( String[] args ) {
for( int i=0;i<57;i++ ) {
new Thread(new Try()).start();
}
}
public Try() {
if( in != null ) return ;
try{
in = new BufferedReader( new FileReader("D:/record.txt") );
}catch( Exception e ){
e.printStackTrace();
}
}
public void run() {
java.sql.Connection con;
java.sql.Statement sta;
try{
Class.forName("com.mysql.jdbc.Driver" );
String url = "jdbc:mysql://localhost:3306/test";
con = java.sql.DriverManager.getConnection(url,"root","password");
String line;
int N=0;
while( (line=in.readLine())!=null ) {
try{
con.createStatement().execute("insert into record values('"+line.trim()+"')" );
N++;
}catch( Exception e ){
System.out.println( "N"+N +e.toString() );
}
}
System.out.println( N );
}catch( Exception e ){
e.printStackTrace();
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货