1

In socket programming, I am trying to send an object to server, then it is throwing ConcurrentModificationException. It is not throwing exception in every iteration. I am trying to send a Serializable object on server.

Code on client -

Message request = new Message();
        request.setMessage("request");
        request.setSourceNode(node);
        System.out.println("cs enter "+ node.getId());
        for(Integer i : clientThread.keySet())
        {
            //clientThread.get(i).sendMessage("request");
            clientThread.get(i).sendMessage(request);
        }

Code on server -

public synchronized void sendMessage(Message send)
     {
         //System.out.println("Client request time stamp" + main.node.getRequestTimestamp());
         send.setDestinationNode(destinationNode);
         //System.out.println(send.getMessage() + " " + send.getSourceNode().getId() + " "+ send.getDestinationNode().getId());
         try {
            oos.writeObject(send);
            oos.reset();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }

StackTrace :

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList.writeObject(ArrayList.java:766)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at Client.sendMessage(Client.java:45)
    at Main.csEnter(Main.java:176)
    at Main.main(Main.java:75)
  • Why send that using the serialization API? Why not use, say, JSON instead? – fge Apr 01 '16 at 18:00
  • Please post the stack trace as well. – NAMS Apr 01 '16 at 18:03
  • A possible reason is that you are changing the number of client threads while traversing it. Including or removing clients while sending the messages. http://stackoverflow.com/questions/4078518/how-to-avoid-concurrentmodificationexception-when-iterating-over-a-map-and-chang – RubioRic Apr 01 '16 at 18:03
  • I am not modifying the client threads hashmap once initialize and using the same every time to send the message. My code is working for first 4 iterations and after that it is throwing this exception. – Nilesh Gupta Apr 01 '16 at 18:08
  • If the exception is thrown from writeObject ... Does Message contain any list? – RubioRic Apr 01 '16 at 18:09
  • Message class : public class Message implements Serializable{ private static final long serialVersionUID = 1L; private String message; private Node sourceNode; private Node destinationNode;} – Nilesh Gupta Apr 01 '16 at 18:10
  • Post the stack trace as suggested by @NAMS – RubioRic Apr 01 '16 at 18:14
  • What about the Node class? Please, update your question with Message and Node class. I've seen the first one already in the comments but may be useful for others. – RubioRic Apr 01 '16 at 18:25
  • I got the issue. In message, I am passing source node and destination node. In those node, one arraylist is modifying that why it throws concurrent exception. I removed that arrayList from the node and it worked. But before closing this thread, can anyone tell me will this issue be there for HashMaps as well....Many thanks for your immediate responses – Nilesh Gupta Apr 01 '16 at 18:46
  • 1
    Yes, it may be produced from a HashMap as well. You can try using ConcurrentHashMap. – RubioRic Apr 01 '16 at 18:49

0 Answers0