在java swing中获取java.util.ConcurrentModificationException

时间:2013-08-30 06:10:26

标签: java exception hashset concurrentmodification

在单击按钮

时多次执行以下代码时获取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);
            }
      }
}

1 个答案:

答案 0 :(得分:0)

    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>");
    }