线程在Eclipse中自动停止(冻结)

时间:2016-07-11 08:47:20

标签: java eclipse multithreading

我在eclipse中运行一个线程来从mysql服务器获取数据。线程工作正常。问题是在一段时间后线程停止运行(持续6到8小时)。线程冻结。之后我必须手动关闭并重新运行程序。 Eclipse在Windows Server 2012 r2计算机上运行。没有显示错误或异常。

主要课程。

public class Main {

      private final static int fONE_DAY = 1;
      private final static int fZERO_MINUTES = 0;

      public static void main(String[] argv) {

        Timer timer1 = new Timer();
        Timer timer4 = new Timer();


        try {
        timer1.scheduleAtFixedRate(new CearteSDQuatation(),500 , 1000*60*4);// 4min
        System.out.println("timer 1 : createSDQuotation");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
        timer4.schedule(new GarbageCol(), 5000, 1000 * 60 * 60);// 60mins
        System.out.println("timer 2 : garbageCollector");
        } catch (Exception e) {
            e.printStackTrace();
        }



}

      private static Date getTomorrowRunningTime(int Ftime){
            Calendar tomorrow = new GregorianCalendar();
            tomorrow.add(Calendar.DATE, fONE_DAY);
            Calendar result = new GregorianCalendar(
              tomorrow.get(Calendar.YEAR),
              tomorrow.get(Calendar.MONTH),
              tomorrow.get(Calendar.DATE),
              Ftime,
              fZERO_MINUTES 
            );
            return result.getTime();
          }




    }

CearteSDQuatation类。

public class CearteSDQuatation extends TimerTask {

    DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");;

    DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    Date date = new Date();

    DBPool_SF pooler;   
    DataSource dataSource;
    DataSource dataSource1;
    DBTableQueryExcecutre qex = null;
    DBTableQueryExcecutre qex3 = null;
    DBTableQueryExcecutre qex1 = null;
    DBTableQueryExcecutre qex2 = null;

    ArrayList<String> dates = null;
    HashSet<String> dateSet = null;
    Iterator<String> itr = null;
    StringBuilder inClause;

    int rcount = 0;



    StringBuilder sbDel = null;
    int delStatus = 0;
    ArrayList<String> elements = new ArrayList<String>();// for update query
    int queryStatus = 0;
    String delete_query1 = " ";
    String strDate = "";
    String strMatnr = "";
    String strkunnr = "";


      // Object for table data
      private Object[][] itemData;


    RFCHandler handler;


      public CearteSDQuatation(){
        handler = new RFCHandler();
        }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try{
        CallItem_ListCreate();
        CallRFC_CreateSDQuata();
        System.out.println("Thread Run");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    private void CallItem_ListCreate() {
        // TODO Auto-generated method stub
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        System.out.println(dateFormat.format(date));

        System.out.println("Create Quotation : Started sub excecutions List create...");
        String sbQuery3 = "SELECT d.plant, h.dis_channel, h.order_no, h.Rep_no," +
                "h.dealer_no,h.order_date,h.last_date, d.items_no," +
                "d.quantity " +
                "FROM tbl_item_list as d, " +
                "tbl_items_header_t as h " +
                "where d.order_no = h.order_no " +
                "and h.status <> 'X'" ;
        try {
        pooler = DBPool_SF.getInstance();
        dataSource1 = pooler.getDataSource();
        System.out.println("pooler");
        } catch (Exception e1)
        {
            e1.printStackTrace();
        }

        try {


        Connection con3 = dataSource1.getConnection();
        con3.setAutoCommit(false);

        Statement st = con3.createStatement();      
        ResultSet rs = st.executeQuery(sbQuery3);





        int lineitem = 0;

        try {
            rs.last();
            rcount = rs.getRow();
            rs.beforeFirst();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("while loop");
        itemData = new Object[9][rcount];

        while(rs.next())
        {           
            itemData[0][lineitem] = rs.getString("plant");
                    lineitem = lineitem + 1;
        }
        } catch (SQLException e)
        {
            e.printStackTrace();
        }



    }

    public void CallRFC_CreateSDQuata()
    {
        System.out.println("Create Quotation : Started sub excecutions");       
        JCO.Table IT_LIST = null;
        JCO.Table IT_LIST1 = null;
        JCO.Table IT_REF = null;
        JCO.Table IT_Msg = null;
        try {

            if(rcount > 0)
            {

            handler.createRFCFunction("ZSL");
            IT_LIST = handler.getTablePara("IT_LIST");  
            IT_LIST1 = handler.getTablePara("IT_LIST1");    

            for(int x = 0; x < rcount; x++ )
            {
                IT_LIST.appendRow();
                IT_LIST1.appendRow();

                IT_LIST.setValue( itemData[0][x].toString().trim(), "SAL_ORG");                     

            }

            handler.excFunction();
            IT_REF = handler.getTablePara("IT_REF");    
            IT_Msg = handler.getTablePara("IT_MSG");
            handler.releaseClient();

            int int_row = IT_REF.getNumRows();

            if (int_row > 0) {
                this.tableOparator(IT_REF , IT_Msg);

            }   
            }

        } catch (Exception ex) {
            handler.releaseClient();
            ex.printStackTrace();
        }

        finally {
            // Release the client to the pool
            //handler.releaseClient();
            rcount = 0;
        }
    }


    public String leadingZeros(String s, int length) {
         if (s.length() >= length) return s;
         else return String.format("%0" + (length-s.length()) + "d%s", 0, s);
    }

    public void tableOparator(JCO.Table table , JCO.Table table1 ) throws Exception {

        pooler = DBPool_SF.getInstance();
        dataSource = pooler.getDataSource();
        Connection con = dataSource.getConnection();
        con.setAutoCommit(false);

        qex = new DBTableQueryExcecutre(con);


        StringBuilder sbQuery = new StringBuilder(
                "INSERT INTO tbl_RefQut (QuatationNo,RefOrderNumber) VALUES");
        System.out.println("COL -->"+table.getNumRows()
                + " records to insert for tbl_RefQut");

        // --- create query ---------------------------------
        for (int i = 0; i < table.getNumRows(); i++) {
            // table.setRow(i);
            sbQuery.append("(?,?),");

        }


        sbQuery.deleteCharAt(sbQuery.length() - 1);
        sbQuery.append(";");
        qex.setUpdateQuery(sbQuery.toString());// *****************
        elements.clear();
        for (int i = 0; i < table.getNumRows(); i++) {
            table.setRow(i);

            elements.add(table.getString("SDOCUMENT").trim());
            elements.add(table.getString("ONUMBER").trim());

        }

            qex.updateInsertQuery(elements);            
            con.commit();




            StringBuilder sbQuery1 = new StringBuilder(
                    "INSERT INTO tbl_item_order_msg (order_no,msg) VALUES");
            System.out.println("COL -->"+table1.getNumRows()
                    + " records to insert for  tbl_item_order_msg");

            // --- create query ---------------------------------
            for (int i = 0; i < table1.getNumRows(); i++) {
                // table.setRow(i);
                sbQuery1.append("(?,?),");

            }


            sbQuery1.deleteCharAt(sbQuery1.length() - 1);
            sbQuery1.append(";");
            qex.setUpdateQuery(sbQuery1.toString());// *****************
            elements.clear();
            for (int i = 0; i < table1.getNumRows(); i++) {
                table1.setRow(i);

                elements.add(table1.getString("ORDER_NUMBER").trim());
                elements.add(table1.getString("MESSAGE").trim());

            }

                qex.updateInsertQuery(elements);            
                con.commit();




            for (int i = 0; i < table.getNumRows(); i++) {
                table.setRow(i);
            String sbQuery2 = "update tbl_items_header_t set status = 'X' where order_no  = '" + table.getString("ORDER_NUMBER").trim() + "';"; 
            int rcount = qex.runQuery(sbQuery2);            
            System.out.println("tbl_items_header_t Rows -->"+rcount + "Status Updated");
            con.commit();
            }


            qex.closeConnections();         

        System.out.println("COL --> Cycle Finished....");
    }


}

1 个答案:

答案 0 :(得分:2)

这种情况的一个可能原因是,您正在点击mysql's max open connections error,因为您没有关闭方法CallItem_ListCreate中的连接。似乎mysql的默认最大连接数是151.你每小时打开15个连接,10小时后你就会遇到那个mentiooned错误。

P.S。 :你应该尝试在调试模式下运行你的代码(或使用像visualvm,jstack这样的工具)并查看@saurav评论的线程转储。