如果我不清楚自己想要什么,请原谅我。我有一个java应用程序,在单击提交时插入2个mysql表,一个是本地机器上的,另一个是在Web服务器上。问题是我创建了两个Threads,每个都有一个。它在第一次启动和启动应用程序时工作正常,但是当应用程序运行并且我尝试通过单击提交再次插入时,它不会执行任何操作。再次当我重新启动应用程序时,它第一次正常工作。我的问题是如何在我的应用程序的同一个实例中停止线程或结束并启动它的新实例。
很抱歉,如果不清楚的话。
继承人的代码,
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)
这听起来不像是一个线程问题。听起来,在将数据写入两个表之后,您不会提交写入或释放数据库连接。根据您将数据写入数据库的方式,可能会发生它与一个线程一起使用,但如果您从多个线程执行此操作则不会。当你第一次运行它时,第一个线程锁定数据库,并且为第二次写入创建的线程无法写入数据,因为第一个线程没有释放锁。
另一件事,每次有人点击按钮时,不是产生两个线程,而是使用ExecutorService
会好得多答案 1 :(得分:1)
我不确定你要用while循环做什么,我不认为你需要它们。尽管如此,第二次使用runt1作为休息条件。可能这不是故意的
答案 2 :(得分: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();
答案 3 :(得分: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
}