2

Why overloaded method writeObject in LinkedList does not checking for ConcurrentModificationException where as ArrayList does.
I seen java code of LinkedList

private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException {
        // Write out any hidden serialization magic
        s.defaultWriteObject();

        // Write out size
        s.writeInt(size);

        // Write out all elements in the proper order.
        for (Node<E> x = first; x != null; x = x.next)
            s.writeObject(x.item);
    }

And java code of ArrayList

private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException{
        // Write out element count, and any hidden stuff
        int expectedModCount = modCount;
        s.defaultWriteObject();

        // Write out size as capacity for behavioural compatibility with clone()
        s.writeInt(size);

        // Write out all elements in the proper order.
        for (int i=0; i<size; i++) {
            s.writeObject(elementData[i]);
        }

        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }

What can be possible reasons.

3 Answers 3

1

First observation: the javadocs for ArrayList and LinkedList don't say if concurrent modification checks are performed while serializing. Therefore, it is consistent with the respective API contracts to do the checks, or not.

So why might they be different?

Without examining the Java source code history all the way back to Java 1.1, we can only guess. It could possibly be an oversight, or an early bug fix that was made in one class and no the other. It could also be that the inconsistency was noticed, but it was not fixed because of concerns that a fix would break customer code.

I couldn't find any related bug reports in the Java Bug Database.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank u Stephen,i have two questions 1. If it is a bug and not yet reported how can we report it, and 2. "fix would break customer code", how it will break? because if we didn't handle it will cause some problem like Little Santi said.
1. It is not a bug. 2. Someone's code might depend on concurrent modifications while a linked list is being serialized not throwing an exception.
1

After analyzing the code, I guess it is clearly a bug in LinkedList:

Notice that the first thing it does is writting on the stream the size of the list, and then its nodes. But, if some node is added/removed while serializing the list, the value of the size already written isn't coherent anymore with the number of nodes actually serialized.

A client that serializes a LinkedList would end up with a ObjectStream that declares 5 elements but has only 4. (And that would eventually produce an exception in readObject.)

2 Comments

Thanks Little Santi, this is exactly my question was ,what happens when added/removed while serializing.so it was a bug?
If, according to my reasoning, the algorithm of LinkedList.writeObject might produce incoherent and bogus information in the serialization stream, I have to conclude: Yes, it is a bug.
0

I think one reason can be that in LinkList throws IndexOutOfBoundException during write ( You can not concurrently write on it) I am not sure and need to test this.

2 Comments

Thanks Amit, but how it can throw IndexOutOfBoundException, because it is not opearting on index it is operating on next address.
Correct but the entry method of LinkedList throws this, you basically insert object to some index location within LinkedList (add, addAll etc call entry method which throws IndexOutOfBoundException)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.