Question

getting java.util.ConcurrentModificationException when the following code get executed several times on clicking the button

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

can anyone tell me some solutions for this

here TABhs is HashSet with PrintWriter generics

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

button click action is shown below

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);
            }
      }
}
Was it helpful?

Solution

    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>");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top