我有一个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往下掉,大家还有什么办法能解决或优化这个线程吗???

解决方案 »

  1.   

    建议楼上的把wait(5000);System.gc();这两句话反过来,同时放在最后,如下:
      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);          
        }
      

  2.   

    我这样做试过,这样做的结果是结少了内存递增的值,原来是几百K几百K往上涨,现在是100K,或20几K或4,5K往上涨,只涨不下
      

  3.   

    mysql="update task_tbl set status=?";
    pstmt=conn.prepareStatement(mysql);
    方到while循环外使用,最好使用addBatch();pstmt=conn.prepareStatement(mysql);
    这里的pstmt再定义一个,一并关闭
      

  4.   


    你这基本是个死循环,当然你可以强制结束或者打断线程
    线程的直接结束方法是 Deprecated
    循环一次涨点内存也很正常
    java的内存回收并不是即时回收你这个虽然用线程,但不是多线程,那为什么又用线程呢
    还有这个 wait(5000); 是什么意思呢,