2013-07-25 20 views
2

我是新手請原諒我,如果我不清楚我想要什麼。我有一個Java應用程序,它在點擊提交時插入到2個mysql表中,其中一個是本地機器,另一個是在Web服務器上。問題是我創建了兩個線程,每個線程一個。它在第一次啓動和啓動應用程序時工作正常,但當應用程序正在運行時,我嘗試通過單擊提交再次插入,它不會執行任何操作。再次,當我重新啓動應用程序,它第一次正常工作。我的問題是如何停止線程或結束並在我的應用程序的同一實例中啓動它的新實例。如何在同一程序中多次創建線程的新實例

對不起,如果我不清楚。

繼承人的代碼,

final int m = MSfuntion.getmemomoc(); 

Thread t1 = new Thread(new Runnable(){ 
public void run() 
{ 
while(runt1){ 

try{ 
     for(int i=0;i<=jTable2.getRowCount();i++) 
      { 
       Object slno= jTable2.getValueAt(i, 1); 
       Object item2= jTable2.getValueAt(i, 2); 
       Object size2= jTable2.getValueAt(i, 3); 

       String it = slno.toString(); 
       String si = item2.toString(); 
       int qt = Integer.parseInt(size2.toString()); 

       MSfuntion.saledetails(m,it, si, qt); 
       //MSfuntion.saledetailsweb(m,it, si, qt); 
       sc=0; 
      } 
     }catch(Exception e) 
     { 
     e.printStackTrace(); 
     } 
    runt1=false; 

    } 
    } 

}); 
Thread t2 = new Thread(new Runnable(){ 

public void run() 
{ 
while(runt2){ 

try{ 
     for(int i=0;i<=jTable2.getRowCount();i++) 
      { 
       Object slno= jTable2.getValueAt(i, 1); 
       Object item2= jTable2.getValueAt(i, 2); 
       Object size2= jTable2.getValueAt(i, 3); 

       String it = slno.toString(); 
       String si = item2.toString(); 
       int qt = Integer.parseInt(size2.toString()); 

       //MSfuntion.saledetails(m,it, si, qt); 
       MSfuntion.saledetailsweb(m,it, si, qt); 
       sc=0; 
      } 
     }catch(Exception e) 
     { 
     e.printStackTrace(); 
     } 
    runt2=false; 
     } 
    t1.start(); 
    t2.start() 
+0

你已經表明將創建2個線程的代碼,這將運行,直到變量'侏儒* N *'是假的(儘管這是邏輯冗餘)在該點theads將終止。從代碼中不清楚'runt * n *'變量是如何設置的,儘管顯然它不在'Runnable's中,如果重複觸發這些代碼,可能會導致不可預知的結果。請顯示所有代碼或指示如何調用此代碼塊。 –

+0

在使用ActionListener – CleanX

+0

單擊jbutton時調用此塊代碼我已經在類的開始處聲明爲「private volatile boolean runt1 = true; private volatile boolean runt2 = true;」。抱歉,不能發佈整個班級,因爲它很大。 – CleanX

回答

2

這聽起來不像是一個線程問題。這聽起來像是在將數據寫入兩個表之後,您不會提交寫入或釋放數據庫連接。根據您將數據寫入數據庫的方式,可能會發生它與一個線程一起工作,但如果您是從多個線程中完成的話,則可能發生這種情況。當您第一次運行時,第一個線程會鎖定數據庫,並且爲第二次寫入創建的線程無法寫入數據,因爲第一個線程沒有釋放鎖。其他

一兩件事,而不是產卵兩個線程每次點擊該按鈕時,它會好得多使用ExecutorService

+0

我已經添加了db函數和ActionListener,請幫助 – CleanX

1

我不知道你正在嘗試用while循環做的,我不認爲你需要他們。儘管如此,第二次使用runt1作爲休息條件。也許這不是故意的

+0

對不起,輸入錯誤,已經糾正它。但是,這並不能解決我的查詢。 – CleanX

1

我相信這個解決您的問題。這是第一次運行程序時,runt1和runt2都可能設置爲true。在第一次運行它們之後,它們將被設置爲false,從而導致while塊被跳過。

final int m = MSfuntion.getmemomoc(); 

Thread t1 = new Thread(new Runnable(){ 
    public void run(){ 
     runt1 = true; 
     while(runt1){ 

      try{ 
       // same as in your code before 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
      runt1=false; 

     } 
    } 

}); 
Thread t2 = new Thread(new Runnable(){ 

    public void run(){ 
     runt2=true; 
     while(runt2){ 

      try{ 
       // your normal code here 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
      runt2=false; 
     } 
    } 
}; 
t1.start(); 
t2.start(); 
+0

'run * n *'和循環沒有任何好處,代碼將表現相同,並且如果它們被刪除則更具可讀性。 –

+0

它設置爲true,不工作:( – CleanX

1

這裏是我的應用程序的數據庫層。

public static void saledetails(int mem, String item2,String size2,int qty2) 
    { 
     try 
     { 
      Connection con = DriverManager.getConnection(GUIbuilding.con,GUIbuilding.usr,GUIbuilding.pass);     

      String sql = "INSERT INTO salesdetails VALUES(?,?,?,?)"; 
      PreparedStatement ps = con.prepareStatement(sql); 

      ps.setInt(1, mem); 
      ps.setString(2, item2); 
    ps.setString(3, size2); 
    ps.setInt(4, qty2);     

      ps.executeUpdate(); 

      String sql2 = "update stock set qty = qty - ? where item= ? && size = ?";     
      PreparedStatement ps1 = con.prepareStatement(sql2); 

      ps1.setInt(1, qty2); 
      ps1.setString(2, item2); 
    ps1.setString(3, size2); 

      ps1.executeUpdate(); 

     } 
     catch(SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    //-------------- sales details------------------------ 

    public static void saledetailsweb(int mem, String item2,String size2,int qty2) 
    { 
     try 
     { // JOptionPane.showMessageDialog(null, GUIbuilding.conweb+"\n"+GUIbuilding.usrweb+"\n"+GUIbuilding.passweb); 
      Connection con =  DriverManager.getConnection(GUIbuilding.conweb,GUIbuilding.usrweb,GUIbuilding.passweb);     



      String sql2 = "update stock set qty = qty - ? where item= ? && size = ?";     
      PreparedStatement ps1 = con.prepareStatement(sql2); 

      ps1.setInt(1, qty2); 
      ps1.setString(2, item2); 
    ps1.setString(3, size2); 

      ps1.executeUpdate(); 

     } 
     catch(SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    //---------------------purchase details---------------------------------------------- 
    public static void purdetails(int mem, String item2,String size2,int qty2) 
    { 
     try 
     { 
      Connection con = DriverManager.getConnection(GUIbuilding.con,GUIbuilding.usr,GUIbuilding.pass);     

      String sql = "INSERT INTO purdetails VALUES(?,?,?,?)"; 
      PreparedStatement ps = con.prepareStatement(sql); 

      ps.setInt(1, mem); 
      ps.setString(2, item2); 
    ps.setString(3, size2); 
    ps.setInt(4, qty2);     

      ps.executeUpdate(); 

      String sql2 = "update stock set qty = qty + ? where item= ? && size = ?";     
      PreparedStatement ps1 = con.prepareStatement(sql2); 

      ps1.setInt(1, qty2); 
      ps1.setString(2, item2); 
    ps1.setString(3, size2); 

      ps1.executeUpdate(); 

     } 
     catch(SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

這裏是整個動作監聽器。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    //sales confirm 
     boolean runt1= true; 
     boolean runt2= true; 
    String cust = jTextField1.getText(); 
    String Is = jTextField2.getText(); 
    //jTextField3.getText(); 
    int sales_order_total_quantity =0; 

    if(cust.equalsIgnoreCase("") || Is.equalsIgnoreCase("") || jTable2.getRowCount()==0) 
    { 

     JOptionPane.showMessageDialog(null, "Please Enter all the required Feilds \n in order to update Sales memo");   
    } 
    else 
    {      
    jTextField1.setText(""); 
    jTextField2.setText(""); 



    try{ 
     if(jTable2.getRowCount()==0) 
     { 
      JOptionPane.showMessageDialog(null,"Please Enter the Items in the sales memo"); 
     } 
     else 
     { 
      for(int i=0;i<=jTable2.getRowCount();i++) 
      { 
       Object slno= jTable2.getValueAt(i, 1); 
       Object item2= jTable2.getValueAt(i, 2); 
       Object size2= jTable2.getValueAt(i, 3); 

       String it = slno.toString(); 
       String si = item2.toString(); 
       int qt = Integer.parseInt(size2.toString()); 
       sales_order_total_quantity += qt; 

       // MSfuntion.saledetails(m,it, si, qt); 
       sc=0; 
      } 
     } 

    } 
    catch(Exception e) 
    { 
    //e.printStackTrace(); 
    } 
    finally 
    { 

MSfuntion.sales(cust, Is,sales_order_total_quantity); 
final int m = MSfuntion.getmemomoc(); 
JOptionPane.showMessageDialog(null, m); 
Thread t1 = new Thread(new Runnable(){ 

public void run() 
{ 
    try{ boolean runt1= true; 
    while(runt1){ 

    try{ 
     for(int i=0;i<=jTable2.getRowCount();i++) 
      { 
       Object slno= jTable2.getValueAt(i, 1); 
       Object item2= jTable2.getValueAt(i, 2); 
       Object size2= jTable2.getValueAt(i, 3); 

       String it = slno.toString(); 
       String si = item2.toString(); 
       int qt = Integer.parseInt(size2.toString()); 
       // JOptionPane.showMessageDialog(null, "Thread is still alive bro"); 
       MSfuntion.saledetails(m,it, si, qt); 
       //MSfuntion.saledetailsweb(m,it, si, qt); 
       sc=0; 
      } 
     }catch(Exception e) 
     { 
     e.printStackTrace(); 
     } 
    runt1=false; 

    } 
    }  catch(Exception e1){e1.printStackTrace();}} 

}); 
Thread t2 = new Thread(new Runnable(){ 

public void run() 
    { boolean runt2= true; 
while(runt2){ 


try{ 
     for(int i=0;i<=jTable2.getRowCount();i++) 
      { 
       Object slno= jTable2.getValueAt(i, 1); 
       Object item2= jTable2.getValueAt(i, 2); 
       Object size2= jTable2.getValueAt(i, 3); 

       String it = slno.toString(); 
       String si = item2.toString(); 
       int qt = Integer.parseInt(size2.toString()); 
       // JOptionPane.showMessageDialog(null, "Thread is still alive bro"); 
       //MSfuntion.saledetails(m,it, si, qt); 
       MSfuntion.saledetailsweb(m,it, si, qt); 
       sc=0; 
      } 
     }catch(Exception e) 
     { 
     e.printStackTrace(); 
     } 
    runt2=false; 



} 
} 

}); 

t1.start(); 
t2.start(); 


    //---------------clear table sales memo--------------------- 
     jTable2.setModel(new javax.swing.table.DefaultTableModel(
     new Object [][] 
     { 

     }, 
     new String [] 
     { 
      "Sl no", "Item", "Size", "Quantity" 
     } 
     ) 
     { 
     Class[] types = new Class [] 
     { 
      java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class 
     }; 

     @Override 
     public Class getColumnClass(int columnIndex) 
     { 
     return types [columnIndex]; 
     } 
     }); 
     //-----------clear table low stock---------------------- 
     jTable1.setModel(new javax.swing.table.DefaultTableModel(
     new Object [][] 
     { 

     }, 
     new String [] 
     { 
      "Item","Size","Quantity" 
     } 
     ) 
     { 
     Class[] types = new Class [] 
     { 
      java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class 
     }; 

     @Override 
     public Class getColumnClass(int columnIndex) 
     { 
     return types [columnIndex]; 
     } 
     }); 
     //--------------------clear table recent sales---------------------------- 
     jTable4.setModel(new javax.swing.table.DefaultTableModel(
     new Object [][] { 

     }, 
     new String [] { 
      "Customer", "Issuer", "Date", "Memo No", "Quantity" 
     } 
    )); 
     MSfuntion.tablepop_sr_recent(jTable4); 
     MSfuntion.tablepop_low(jTable1); 

    }} 
    //end of listener 

} 
相關問題