Is List.iterator() thread-safe?

26,248

Solution 1

The behaviour of List.iterator() is not defined or consistent with different List implementations.

For ArrayList, LinkedList, you can get a ConcurrentModificationException if the list is modified when you are iterating over it. (This is not guaranteed) The way to avoid this issue is to use a synchronizedList() and lock the list while iterating over it.

For Vector, the collection is synchronized, but the iterator is not thread safe.

For CopyOnWriteArrayList, you get a snapshot of the elements in the list at the time you call iterator(), This iterator is thread safe, and you don't need to use any locking. Note: the contents of the elements can change.

Solution 2

No iterator is thread-safe. If the underlying collection is changed amidst iteration, a ConcurrentModificationException is thrown.

Even iterators of synchronized collections are not thread-safe - you have to synchronize manually.

One exception is the CopyOnWriteArrayList, which holds a snapshot during iteration.

Share:
26,248
ubuntudroid
Author by

ubuntudroid

Android engineer since 2009 (fragmentation was not an issue back then as there was just the emulator - good times :D). Productivity geek - I have probably tried out all relevant productivity systems and apps out there. My dark bane, but at the same time my biggest driver. Also avid gamer, history buff, tech lover and Indiegogo/Kickstarter addict. Always on the hunt for new technologies and optimizations - cannot get enough of new, fancy, geeky stuff! If I build things myself the most important thing is: the product of my work has to be of great usability, expandability and maintainability as well as ethically and morally unquestionable (no greyzones here!). In most situations my target groups are end users as well as other developers. I tend to spend an equal amount of time on code optimizations regarding flexibility and expandability of software projects as on the development of new features. Less than the achievement of these goals is usually not acceptable for me - and I'm motivating my collaborators to behave like this, too. Nevertheless I'm not a pedant and tend to behave pragmatically. And I'm a firm believer of teamwork and team spirit: together two people can achieve way more than each of them alone - but equally important are the times where you completely disconnect and just focus. The latter being one of the reasons why I firmly believe in the idea of working remotely. In the last two years I've also invested quite some time into iOS development, specifically with Kotlin Native. And lately one more thing is bothering me more and more: "meaning" in what I'm doing and how. For me that boils down to improving the world we live in - even if it is just a tiny bit. We make thousands of decisions every day and many of them just mindlessly choosing to ignore all the tiny opportunities there are to change the state of things - just because we do what everyone does and hide behind that. People may call me boring looking at my pretty conservative lifestyle, but there are possibilities for tiny little revolutions in everything we do - in every smile when we enter a room, every encouragement we choose to give, spending time with our family instead of our phone (and oh my, I'm struggling quite a bit here), and, yes, also every piece of user data we choose to NOT forward to third party services when writing user facing software. Many small decision which alone themselves don't change the world - but imagine we all chose to make every fifth decision mindfully... Certainly nothing I myself could ever hope to achieve, but better die trying than not trying at all. That's what provides meaning to my life, even if I fail at least a dozen times a day. Find me on LinkedIn: https://www.linkedin.com/in/svenbendel/.

Updated on June 27, 2020

Comments

  • ubuntudroid
    ubuntudroid almost 4 years

    In Java: Is List.iterator() thread-safe, i.e. does the returned iterator reflect the current state of the list at any time or just the state of the list at the time of its creation?