Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it a good way to prevent ConcurrentModificationException
    primarykey
    data
    text
    <p>The another question is about <strong>synchronized</strong>. I have also a run() in class Note,because i want to output each element in notes every 5 minutes. But i get always exception:java.util.ConcurrentModificationException,if i try to make more meetings in main. so i applay synchronized to the list notes which may be added a new meeting when i iterate over notes.My run method like this:</p> <p><strong>Is it correct way hier on list notes to synchronized to prevent ConcurrentModificationException ?</strong>(In my program it works.I get never this exception now)</p> <p>A Meeting class and Note class may likes this:</p> <pre><code>public class Meeting{ public Meeting(Note note_1,Note note_2){ note_1.addElement(this); note_2.addElement(this);} //another method hier } public class Note implements Runnable{ public final List&lt;Meeting&gt; notes = new ArrayList&lt;Meeting&gt;(); public void addElement(Meeting n){ entries.add(n); } @Override public void run(){ while(true) { for(Meeting n : notes){ System.out.println(n.toString);} } try{ Thread.sleep(10);} } } } </code></pre> <p>I get always exception error about <strong>exception:java.util.ConcurrentModificationException</strong> if i try to make more Meeting in main ,So i changes a littel in class Note,likes this :</p> <pre><code> private static final List&lt;Entry&gt; entries = Collections.synchronizedList(new ArrayList&lt;Entry&gt;()); </code></pre> <p>and in run()</p> <pre><code>@Override public void run() { while(true){ synchronized(notes){ for(Entry n : entries){ //do something } } try { Thread.sleep(10); } catch (InterruptedException e ) { } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    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. 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