On Tue, 24 Feb 2026 18:45:29 GMT, Jason Mehrens <[email protected]> wrote:
> If the given collection 'c' is Collections.synchronziedXXX, then simple loop
> doesn't lock correctly. ForEach and toArray work correctly with that wrapper
> class.
The synchronized wrapper collections clearly specify that the user is
responsible for synchronizing any iteration or stream operations on those
collections. Then, who is the user in this case? The one creating and passing
synchronzed collections around, or do we include any collection in the
collections framework?
To avoid further headscratching about this, perhaps a good old anonymous class
would be less risky?
c.forEach(new Consumer<E>() {
@Override
public void accept(E e) {
addLast(e);
}
});
-------------
PR Comment: https://git.openjdk.org/jdk/pull/29894#issuecomment-3954620028