Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Normally no because you are not changing the internal state of the collection in this case. When you iterate over the collection a new instance of the iterator is created and the state of the iteration is per iterator instance.</p> <hr> <p><strong>Aside note:</strong> Remember that by keeping a read-only collection you are only preventing modifications to the collection itself. Each collection element is still changeable.</p> <pre><code>class Test { public Test(final int a, final int b) { this.a = a; this.b = b; } public int a; public int b; } public class Main { public static void main(String[] args) throws Exception { List&lt;Test&gt; values = new ArrayList&lt;Test&gt;(2); values.add(new Test(1, 2)); values.add(new Test(3, 4)); List&lt;Test&gt; readOnly = Collections.unmodifiableList(values); for (Test t : readOnly) { t.a = 5; } for (Test t : values) { System.out.println(t.a); } } } </code></pre> <p>This outputs:</p> <pre><code>5 5 </code></pre> <hr> <p>Important considerations from @WMR answser.</p> <blockquote> <p>It depends on if the threads that are reading your collection are started before or after you're filling it. If they're started before you fill it, you have no guarantees (without synchronizing), that these threads will ever see the updated values. </p> <p>The reason for this is the Java Memory Model, if you wanna know more read the section "Visibility" at this link: <a href="http://gee.cs.oswego.edu/dl/cpj/jmm.html" rel="nofollow noreferrer">http://gee.cs.oswego.edu/dl/cpj/jmm.html</a></p> <p>And even if the threads are started after you fill your collection, you might have to synchronize because your collection implementation could change its internal state even on read operations (thanks <a href="https://stackoverflow.com/questions/152342/in-java-do-i-need-to-declare-my-collection-synchronized-if-its-read-only#152425">Michael Bar-Sinai</a>, I didn't know such collections existed).</p> <p>Another very interesting read on the topic of concurrency which covers topics like publishing of objects, visibility, etc. in much more detail is Brian Goetz's book <a href="http://www.briangoetz.com/pubs.html" rel="nofollow noreferrer">Java Concurrency in Practice</a>.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload