多线程数据库插入死锁
package com.test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
public class ThreadRead{
/**Java多线程读大文件
* @param args
*/
public static void main(String[] args) {
int start=(int)System.currentTimeMillis();
System.out.println(start+"");
Thread t1=new Thread(new MultiThreadDemo(),"A");
Thread t2=new Thread(new MultiThreadDemo(),"B");
t1.start();
t2.start();
int end=(int)System.currentTimeMillis();
System.out.println(end+"");
System.out.println((end-start)+" ");
}
}
class MultiThreadDemo implements Runnable{
private static BufferedReader br = null;
private List<String> list;
static{
try {
br = new BufferedReader(new FileReader("E:\\lineitem.tbl"),1024);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void display(List<String> list) {
int count = 0;
System.out.println(list.size());
for(String str:list) {
String[] arrtest = str.split("\\|");
count++;
String sql="insert into newtable (TableName,TupleNum,PropertyName,ElementValue) values (?,?,?,?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = testThread.getConnection();
conn.setAutoCommit(false);
pstmt = testThread.getPstmt(conn, sql);
String recordName[]={"L_ORDERKEY","L_PARTKEY","L_SUPPKEY","L_LINENUMBER","L_QUANTITY",
"L_EXTENDEDPRICE","L_DISCOUNT","L_TAX","L_RETURNFLAG","L_LINESTATUS","L_SHIPDATE"
,"L_COMMITDATE", "L_RECEIPTDATE","L_SHIPINSTRUCT","L_SHIPMODE","L_COMMENT"};
for(int i=0;i<arrtest.length;i++){
pstmt.setString(1, "LINEITEM");
pstmt.setInt(2, count);
pstmt.setString(3, recordName[i]);
pstmt.setString(4, arrtest[i]);
pstmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
testThread.rollback(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
testThread.close(pstmt);
testThread.close(conn);
}
// System.out.println(str);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
String line = null;
int count=0;
while(true) {
this.list = new ArrayList<String>();
synchronized(br) {
try {
while((line = br.readLine()) != null) {
if(count<15) {
list.add(line);
count++;
}else {
list.add(line);
count = 0;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1);
display(this.list);
} catch (Exception e) {
e.printStackTrace();
}
if(line == null)
break;
}
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
public class ThreadRead{
/**Java多线程读大文件
* @param args
*/
public static void main(String[] args) {
int start=(int)System.currentTimeMillis();
System.out.println(start+"");
Thread t1=new Thread(new MultiThreadDemo(),"A");
Thread t2=new Thread(new MultiThreadDemo(),"B");
t1.start();
t2.start();
int end=(int)System.currentTimeMillis();
System.out.println(end+"");
System.out.println((end-start)+" ");
}
}
class MultiThreadDemo implements Runnable{
private static BufferedReader br = null;
private List<String> list;
static{
try {
br = new BufferedReader(new FileReader("E:\\lineitem.tbl"),1024);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void display(List<String> list) {
int count = 0;
System.out.println(list.size());
for(String str:list) {
String[] arrtest = str.split("\\|");
count++;
String sql="insert into newtable (TableName,TupleNum,PropertyName,ElementValue) values (?,?,?,?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = testThread.getConnection();
conn.setAutoCommit(false);
pstmt = testThread.getPstmt(conn, sql);
String recordName[]={"L_ORDERKEY","L_PARTKEY","L_SUPPKEY","L_LINENUMBER","L_QUANTITY",
"L_EXTENDEDPRICE","L_DISCOUNT","L_TAX","L_RETURNFLAG","L_LINESTATUS","L_SHIPDATE"
,"L_COMMITDATE", "L_RECEIPTDATE","L_SHIPINSTRUCT","L_SHIPMODE","L_COMMENT"};
for(int i=0;i<arrtest.length;i++){
pstmt.setString(1, "LINEITEM");
pstmt.setInt(2, count);
pstmt.setString(3, recordName[i]);
pstmt.setString(4, arrtest[i]);
pstmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
testThread.rollback(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
testThread.close(pstmt);
testThread.close(conn);
}
// System.out.println(str);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
String line = null;
int count=0;
while(true) {
this.list = new ArrayList<String>();
synchronized(br) {
try {
while((line = br.readLine()) != null) {
if(count<15) {
list.add(line);
count++;
}else {
list.add(line);
count = 0;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1);
display(this.list);
} catch (Exception e) {
e.printStackTrace();
}
if(line == null)
break;
}
}
}
...全文
相关推荐
SQL数据库死锁知识
SQL Server死锁总结,死锁原理、死锁排查、避免死锁及死锁处理方法,有死锁实例及解决方法
封装好的berbeley db 多线程API 源码(c++)
自己写的bdb多线程接口, 打开数据库。然后就可以调用set和get来存储数据。
Visual C++数据库实用编程100例.((铁道出版.崔莹)(配书源码)
《Visual C++数据库实用编程100例》(中国铁道出版社.崔莹.王华军.姚雪峰)一书的配套源代码。
Java操作Oracle数据库-多线程.rar
Java操作Oracle数据库-多线程,演示了使用多线程技术从oracle 数据库中读取函数、存储过程等功能。,具体实现方法请详见源代码。
java学习案例,实现多线程,数据库,网络编程
读取txt文本文件,将数据以多线程通过socket网络传输存储到另一个客户端的mysql数据库中,整个过程使用就AVA,mysql,仅供学习参考


