我有一个APPLICATION
DataSource ds=DataConnection.getInstance().ds();
Connection conn=ds.getConnection();
这些是程序刚启动时一次性设置的,没什么多说的
关键的在于下面,前面的MAIN把设置好的CONN传给下一个FRAME,然后在下一个FRAME,叫sendframe.class里有一个BUTTON,一点这个BUTTON,会启动一个线程,这个线程是一直循环的,如下public class SendThread extends Thread{
private Connection conn=null;
public void SendThread(Connection conn){
this.conn=conn;
}
public void run(){
while(!interrupt()){
dealData();
}
}
private void dealData(){
PreparedStatement pstmt=null;
ResultSet rs=null;
String mysql="";
try{
mysql="select * from task_tbl where try_times<3 and status=?";
pstmt=conn.prepareStatement(mysql);
pstmt.setString(1,"0");
rs=pstmt.executeQuery();
while(rs.next()){
String task_id=rs.getString("t_id");
String phoneNum=rs.getString("cell_phone");
sendSms(t_id,phoneNum);
mysql="update task_tbl set status=?";
pstmt=conn.prepareStatement(mysql);
pstmt.setString(1,"1");
}
wait(5000);
System.gc();
}catch(Exception ex){}
finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
}catch(Exception e){}
try{
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
}catch(Exception e){}
}}
以上就是这个程序的主体,有一个问题我要请教大家,就是这个线程一旦RUN了,尽管我在很多方面做过优化,但是我打开WINDOWS的进程监视器,还有在命令行中加入了verbosegc后,就发觉此程序的内存16K,14K`不等的会往上增加,没见1K往下掉,大家还有什么办法能解决或优化这个线程吗???
DataSource ds=DataConnection.getInstance().ds();
Connection conn=ds.getConnection();
这些是程序刚启动时一次性设置的,没什么多说的
关键的在于下面,前面的MAIN把设置好的CONN传给下一个FRAME,然后在下一个FRAME,叫sendframe.class里有一个BUTTON,一点这个BUTTON,会启动一个线程,这个线程是一直循环的,如下public class SendThread extends Thread{
private Connection conn=null;
public void SendThread(Connection conn){
this.conn=conn;
}
public void run(){
while(!interrupt()){
dealData();
}
}
private void dealData(){
PreparedStatement pstmt=null;
ResultSet rs=null;
String mysql="";
try{
mysql="select * from task_tbl where try_times<3 and status=?";
pstmt=conn.prepareStatement(mysql);
pstmt.setString(1,"0");
rs=pstmt.executeQuery();
while(rs.next()){
String task_id=rs.getString("t_id");
String phoneNum=rs.getString("cell_phone");
sendSms(t_id,phoneNum);
mysql="update task_tbl set status=?";
pstmt=conn.prepareStatement(mysql);
pstmt.setString(1,"1");
}
wait(5000);
System.gc();
}catch(Exception ex){}
finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
}catch(Exception e){}
try{
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
}catch(Exception e){}
}}
以上就是这个程序的主体,有一个问题我要请教大家,就是这个线程一旦RUN了,尽管我在很多方面做过优化,但是我打开WINDOWS的进程监视器,还有在命令行中加入了verbosegc后,就发觉此程序的内存16K,14K`不等的会往上增加,没见1K往下掉,大家还有什么办法能解决或优化这个线程吗???
解决方案 »
- JDBC的问题!!!!!
- Thread sleep疑惑
- 请问eclipse如何关闭括号匹配与代码补全
- 问个JAVA第一次求职的问题!
- java能监听图像吗
- JAVA 初学者怎样才能JAVA高手?我想学WEB开发.能介绍一下应该学习哪些方面的东西,和推荐一些相关书籍吗?
- 怎么写个正规式把下面的连接提取出来??
- java能不能开发c/s结构的系统用什么工具??急
- 我要做一个类似与IE的浏览器!各位帮我!
- 让我头疼的问题,帮我解决,谢谢!
- 请问怎样用java调用可执行文件,如war3.exe,谢谢!
- 将字符串里:< 替换成< > 替换成> & 替换成& ' 替换成'" 替换成"用什么样的正则呢?
private void dealData(){
PreparedStatement pstmt=null;
ResultSet rs=null;
String mysql="";
try{
mysql="select * from task_tbl where try_times<3 and status=?";
pstmt=conn.prepareStatement(mysql);
pstmt.setString(1,"0");
rs=pstmt.executeQuery();
while(rs.next()){
String task_id=rs.getString("t_id");
String phoneNum=rs.getString("cell_phone");
sendSms(t_id,phoneNum);
mysql="update task_tbl set status=?";
pstmt=conn.prepareStatement(mysql);
pstmt.setString(1,"1");
}
}catch(Exception ex){}
finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
}catch(Exception e){}
try{
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
}catch(Exception e){}
System.gc();
wait(5000);
}
pstmt=conn.prepareStatement(mysql);
方到while循环外使用,最好使用addBatch();pstmt=conn.prepareStatement(mysql);
这里的pstmt再定义一个,一并关闭
你这基本是个死循环,当然你可以强制结束或者打断线程
线程的直接结束方法是 Deprecated
循环一次涨点内存也很正常
java的内存回收并不是即时回收你这个虽然用线程,但不是多线程,那为什么又用线程呢
还有这个 wait(5000); 是什么意思呢,