gpt4 book ai didi

java - 在 java swing 中获取 java.util.ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-01 04:25:39 25 4
gpt4 key购买 nike

当单击按钮多次执行以下代码时,出现java.util.ConcurrentModificationException

for(Iterator <PrintWriter> it=TABhs.iterator();it.hasNext();)
{
PrintWriter ot=it.next();
ot.println("tableupdate#"+tables+"#"+kotno+"#processing");
ot.flush();
JOptionPane.showMessageDialog(rootPane, "<html><body>Table Kot Status Changed to <b>Processing</b></body></html>");
}

谁能告诉我一些解决方案

这里 TABhs 是带有 PrintWriter 泛型的 HashSet

HashSet <PrintWriter> TABhs=new HashSet<PrintWriter>();

按钮点击操作如下所示

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String stat=status_combo.getSelectedItem().toString();
String tables=tableno_combo.getSelectedItem().toString();
String kotno=kotno_combo.getSelectedItem().toString();

if(stat.equals("Processing"))
{
try {
String quer="UPDATE table_orders SET status='"+stat.toLowerCase()+"' WHERE tableno='"+tables+"' AND kotno='"+kotno+"'";
int restatus=dbGetDet.insertDetails(quer);
if(restatus>0){
filtercomboBox();
fillTable();
for(Iterator <PrintWriter> it=TABhs.iterator();it.hasNext();)
{
PrintWriter ot=it.next();
ot.println("tableupdate#"+tables+"#"+kotno+"#processing");
ot.flush();
JOptionPane.showMessageDialog(rootPane, "<html><body>Table Kot Status Changed to <b>Processing</b></body></html>");
}



System.out.println("TABhs--------------------->"+TABhs.size());
}
} catch (Exception ex) {
Logger.getLogger(MYClientclass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

最佳答案

    TABhs = new CopyOnWriteArraySet(TABhs);
for(Iterator <PrintWriter> it=TABhs.iterator();it.hasNext();)
{
PrintWriter ot=it.next();
ot.println("tableupdate#"+tables+"#"+kotno+"#processing");
ot.flush();
JOptionPane.showMessageDialog(rootPane, "<html><body>Table Kot Status Changed to <b>Processing</b></body></html>");
}

关于java - 在 java swing 中获取 java.util.ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18526124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com