Java 在程序中使用线程池
您好,我有一个java程序和JDBC,我在其中使用了线程,但我遇到了如下异常:Java 在程序中使用线程池,java,linux,jdbc,threadpool,Java,Linux,Jdbc,Threadpool,您好,我有一个java程序和JDBC,我在其中使用了线程,但我遇到了如下异常: Exception in thread "Thread-1964" java.lang.OutOfMemoryError: Java heap space 我想,因为我无限地开始线程,而不是关闭线程 因此,我想使用线程池,即线程来自池的sp,在执行任务后,它将打包到池中 这是我的java代码: public class DBTestCases{ Connection localConnection;
Exception in thread "Thread-1964" java.lang.OutOfMemoryError: Java heap space
我想,因为我无限地开始线程,而不是关闭线程
因此,我想使用线程池,即线程来自池的sp,在执行任务后,它将打包到池中
这是我的java代码:
public class DBTestCases{
Connection localConnection;
Connection remoteConnection;
Connection localCon;
Connection remoteCon;
List<Connection> connectionsList;
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "root";
String dbName = "myDB";
String connectionUrl1= "jdbc:mysql://11.232.33:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
String connectionUrl2= "jdbc:mysql://localhost:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
public List<Connection> createConnection() {
try {
Class.forName(driver);
localCon = DriverManager.getConnection(connectionUrl2);
if(localCon != null)
System.out.println("connected to remote database at : "+new Date());
remoteCon = DriverManager.getConnection(connectionUrl1);
if(remoteCon != null)
System.out.println("connected to local database at : "+new Date());
connectionsList = new ArrayList<Connection>( 2 );
connectionsList.add( 0 , localCon );
connectionsList.add( 1 , remoteCon );
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch(SQLException sqle) {
sqle.printStackTrace();
}
return connectionsList;
}
public void insert(){
Runnable runnable = new Runnable(){
public void run() {
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
String sql = "insert into user1(name, address, created_date)" +
" values('johnsan', 'usa', '2013-08-04')";
if(remoteConnection != null&&localConnection != null) {
System.out.println("Database Connection Is Established");
try {
ps1 = remoteConnection.prepareStatement(sql);
ps2 = localConnection.prepareStatement(sql);
int i = ps1.executeUpdate();
int k = ps2.executeUpdate();
if(i > 0) {
System.out.println("Data Inserted into remote database table Successfully");
}
if(k > 0) {
System.out.println("Data Inserted into local database table Successfully");
}
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
s.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Inserting values in db");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public void retrieve(){
Runnable runnable = new Runnable(){
public void run() {
try {
Statement st1 = localConnection.createStatement();
Statement st2 = remoteConnection.createStatement();
ResultSet res1 = st1.executeQuery("SELECT * FROM user1");
ResultSet res2 = st2.executeQuery("SELECT * FROM user1");
System.out.println("---------------------------Local Database------------------------");
while (res1.next()) {
Long i = res1.getLong("userId");
String s1 = res1.getString("name");
String s2 = res1.getString("address");
java.sql.Date d = res1.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
System.out.println("------------------------Remote Database---------------------");
while (res2.next()) {
Long i = res2.getLong("userId");
String s1 = res2.getString("name");
String s2 = res2.getString("address");
java.sql.Date d = res2.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
s.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public static void main(String[] args) {
DBTestCases dbTestCases = new DBTestCases();
List l = dbTestCases.createConnection();
dbTestCases.localConnection = (Connection)l.get(0);
dbTestCases.remoteConnection = (Connection)l.get(1);
for(;;) {
dbTestCases.insert();
dbTestCases.countRows();
dbTestCases.retrieve();
}
}
}
公共类DBTestCases{
连接本地连接;
连接远程连接;
连接localCon;
远程连接;
列表连接列表;
String driver=“com.mysql.jdbc.driver”;
字符串user=“root”;
字符串password=“root”;
字符串dbName=“myDB”;
String connectionUrl1=“jdbc:mysql://11.232.33:3306/“+dbName+”?user=“+user+”&password=“+password+”&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10”;
String connectionUrl2=“jdbc:mysql://localhost:3306/“+dbName+”?user=“+user+”&password=“+password+”&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10”;
公共列表createConnection(){
试一试{
Class.forName(驱动程序);
localCon=DriverManager.getConnection(connectionUrl2);
如果(localCon!=null)
System.out.println(“连接到远程数据库:“+new Date()”);
remoteCon=DriverManager.getConnection(connectionUrl1);
if(remoteCon!=null)
System.out.println(“连接到本地数据库:“+new Date()”);
connectionsList=newarraylist(2);
connectionsList.add(0,localCon);
connectionsList.add(1,remoteCon);
}捕获(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}捕获(SQLException sqle){
printStackTrace();
}
返回连接列表;
}
公开作废插入(){
Runnable Runnable=新的Runnable(){
公开募捐{
PreparedStatement ps1=null;
PreparedStatement ps2=null;
String sql=“插入user1(名称、地址、创建日期)”+
“价值观(‘约翰森’、‘美国’、‘2013-08-04’)”;
if(remoteConnection!=null&&localConnection!=null){
System.out.println(“已建立数据库连接”);
试一试{
ps1=remoteConnection.prepareStatement(sql);
ps2=localConnection.prepareStatement(sql);
int i=ps1.executeUpdate();
int k=ps2.executeUpdate();
如果(i>0){
System.out.println(“数据成功插入远程数据库表”);
}
如果(k>0){
System.out.println(“数据成功插入本地数据库表”);
}
}捕获(SQLS异常){
System.out.println(“SQL代码不执行”);
s、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
System.out.println(“在db中插入值”);
}
};
线程线程=新线程(可运行);
thread.start();
}
公共无效检索(){
Runnable Runnable=新的Runnable(){
公开募捐{
试一试{
语句st1=localConnection.createStatement();
语句st2=remoteConnection.createStatement();
ResultSet res1=st1.executeQuery(“从user1中选择*);
ResultSet res2=st2.executeQuery(“从用户1中选择*);
System.out.println(“-------------------------------本地数据库------------------------------------------”;
while(res1.next()){
Long i=res1.getLong(“userId”);
字符串s1=res1.getString(“名称”);
字符串s2=res1.getString(“地址”);
java.sql.Date d=res1.getDate(“创建日期”);
System.out.println(i+“\t\t”+s1+“\t\t”+s2+“\t\t”+d);
}
System.out.println(“---------------------------远程数据库------------------------------------------”;
while(res2.next()){
Long i=res2.getLong(“userId”);
字符串s1=res2.getString(“名称”);
字符串s2=res2.getString(“地址”);
java.sql.Date d=res2.getDate(“创建日期”);
System.out.println(i+“\t\t”+s1+“\t\t”+s2+“\t\t”+d);
}
}捕获(SQLS异常){
System.out.println(“SQL代码不执行”);
s、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
};
线程线程=新线程(可运行);
thread.start();
}
公共静态void main(字符串[]args){
DBTestCases DBTestCases=新的DBTestCases();
List l=dbTestCases.createConnection();
dbTestCases.localConnection=(Connection)l.get(0);
dbTestCases.remoteConnection=(Connection)l.get(1);
对于(;;){
dbTestCases.insert();
dbTestCases.countRows();
dbTestCases.retrieve();
}
}
}
请告诉我应该如何修改这个程序以使用线程池..这样我就不会得到那个异常
提前感谢您使用ExecutorService创建线程池: 例如,ThreadPoolExecutor: ,在大多数情况下,您需要JDBC公司