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()
4

4 回答 4

2

对我来说,这听起来不像是线程问题。听起来您在将数据写入两个表后都没有提交写入或释放数据库连接。根据您将数据写入数据库的方式,它可能会与一个线程一起使用,但如果您从多个线程中执行则不会。第一次运行时,第一个线程锁定数据库,为第二次写入创建的线程无法写入数据,因为第一个线程没有释放锁。

另一件事,不是每次有人单击按钮时产生两个线程,而是使用ExecutorService会好得多

于 2013-07-25T11:28:15.677 回答
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();
于 2013-07-25T11:37:20.357 回答
1

我不确定你想用 while 循环做什么,我认为你不需要它们。然而,第二个 while 使用 runt1 作为中断条件。可能不是故意的

于 2013-07-25T11:26:45.237 回答
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

}  
于 2013-07-25T19:20:56.133 回答