Update  As was pointed out by Jiri Tousek, the error that was being thrown in my code has misled many amateur (and experienced) Java developers. Contrary to what the name seems to imply, ConcurrentModificationException does not have anything to do with multi-threading. Consider the following code:  import java.util.List; import java.util.ArrayList;  class Main {   public static void main(String[] args) {     List&lt;String&gt; originalArray = new ArrayList&lt;&gt;();     originalArray.add("foo");     originalArray.add("bar");     List&lt;String&gt; arraySlice = originalArray.subList(0, 1);     originalArray.remove(0);     System.out.println(Integer.toString(arraySlice.size()));   } }   This will throw a ConcurrentModificationException despite there being no threading involved.  The misleading exception name led me to think my problem was the result of how I was handling multi-threading. I've updated the title of my post with the actual issue.  Original (title: How to inform Java that you are finished modifying an ArrayList in a thread?)  I have code that looks roughly like the following:  class MessageQueue {     private List&lt;String&gt; messages = new ArrayList&lt;&gt;();     private List&lt;String&gt; messagesInFlight = new ArrayList&lt;&gt;();      public void add(String message) {         messages.add(message);     }      public void send() {         if (messagesInFlight.size() &gt; 0) {             // Wait for previous request to finish             return;         }          messagesInFlight = messages.subList(0, Math.min(messages.size, 10));         for( int i = 0; i &lt; messagesInFlight.size(); i++ )         {             messages.remove(0);         }          sendViaHTTP(messagesInFlight, new Callback() {             @Override             public void run() {                 messagesInFlight.clear();             }         });     } }   This is utilized in my code for analytics purposes. Every 10 seconds I call messageQueue.send() from a timer, and whenever an event of interest occurs I call messageQueue.add(). This class works *for the most part* -- I can add messages and they get sent via HTTP, and when the HTTP request completes the callback is run  The issue lies on the second tick of the timer. When I hit the line if (messagesInFlight.size() &gt; 0) {, I get the following error:  java.util.ConcurrentModificationException         at java.util.ArrayList$SubList.size(ArrayList.java:1057)   It seems like I can't read the .size() of the array in one thread (the second timer's callback) because it thinks the array is still being modified by the other thread (the first timer's callback). However I would expect the first timer's thread was destroyed and cleaned up after my call to sendViaHTTP, since there was no additional code for it to execute. Furthermore, the HTTP request is completing within 500 milliseconds, so a full 9.5 seconds passes without anything touching the empty messagesInFlight array  Is there a way to say say "hey, I'm done modifying this array, people can safely read it now"? Or perhaps a better way to organize my code?
We all know you can't do the following because of ConcurrentModificationException:  for (Object i : l) {     if (condition(i)) {         l.remove(i);     } }   But this apparently works sometimes, but not always. Here's some specific code:  public static void main(String[] args) {     Collection&lt;Integer&gt; l = new ArrayList&lt;&gt;();      for (int i = 0; i &lt; 10; ++i) {         l.add(4);         l.add(5);         l.add(6);     }      for (int i : l) {         if (i == 5) {             l.remove(i);         }     }      System.out.println(l); }   This, of course, results in:  Exception in thread "main" java.util.ConcurrentModificationException   Even though multiple threads aren't doing it. Anyway.  What's the best solution to this problem? How can I remove an item from the collection in a loop without throwing this exception?  I'm also using an arbitrary Collection here, not necessarily an ArrayList, so you can't rely on get.
Ubuntu 14.04 has visual artifacts and freezes So from what I understand transistors are one of the most important electronic elements for computers because of TTL (Transistors-Transistors Logic).  Is this true? If so, how exactly does this work? I have heard the terms "logic gate" and "logic block" tossed around. What is The Rule of Three? How can I find color codes on Mac? I used to find color codes which I want to use for texts or background colors on htmls with color edit tool of MS paint which pre-installed on Windows. But now I use Mac instead of Windows. How can I find color codes with Mac? Is an software like MS paint pre-installed on Mac? Number of elements of a finite field Computing the Galois group of polynomials $x^n-a \in \mathbb{Q}[x]$ Why do video cables (DVI, VGA) still screw in? I guess I could imagine that there was a time when accidentally disconnecting the monitor would have been catastrophic, though I don't see that being the case today.  Is it just a legacy holdover that they are still secured with screws?  Also, why is it that the video connection is really the only one that is still secured this way?  It seems like the power cable would be a better candidate to be secured with screws.  That really could be catastrophic.  In the end, the reason this question occurred to me is that I've never had a video cable come out when not screwed in, so why the significant security? I have 41 GB of space on my E:/ drive. Is it sufficient to install Ubuntu? My Windows XP is installed in C:/ drive. Is it OK for dual boot? Should I install Ubuntu (I am installing using Live CD). I don't want to lose my Windows option. I've got a development webserver on an Ubuntu box inside of VMWare Workstation 7.1.3. I'd like to run a web browser on a Windows machine (the host OS) for actually testing a web page I'm working on -- hard to run IE tests in Ubuntu :)  However, if I simply try to ping the IP given to the ubuntu VM (192.168.83.137) (or open it in my browser for that matter), it seems the host cannot communicate with the guest.  How do I allow the guest OS to communicate with the host when the guest is running in NAT mode? How to find windows 10 activation Key Where can it be find?  At activation panel i can only find option to change the activation key. The current key is not presented on this screen. Ubuntu files worthless on Windows? When I was using ubuntu 14.04, it's easy to set title. Just press F2 or move mouse to top menu. But there is no menu on ubuntu 16.04 and press F2 can't change the title. What should I do? Proof by Induction: For any integer $n > 0$, $3|(5^{2n} − 1)$. When to use LinkedList over ArrayList in Java? The timeout period elapsed prior to obtaining a connection from the pool I'm getting this error every few days. I won't see the error for a few days then I'll get a flurry of 20 or so all with in a minute or so.  I've been very thorough going throw my code so that I'm using this basic setup for my DB access.  try {   myConnection.Open();   mySqlDataAdapter.Fill(myDataTable);   myConnection.Close(); } Catch (Exception err) {   if (myConnection.State != ConnectionState.Closed) myConnection.Close();   throw err; }   The way I understand it this should execute my queries and immediately release the connection back to the pool but if something goes wrong with the query then I catch the excpetion close my connection then throw the error up, which eventually gets trapped at the application level and logs and emails me the error.  Even using this throughout my code I'm still running across the issue. What can I do to diagnose the root cause of the issue? @future method: Unsupported parameter type The diophantine equation $x^2+y^2=3z^2$ I tried to solve this question but without success:     Find all the integer solutions of the equation: $x^2+y^2=3z^2$   I know that if the sum of two squares is divided by $3$ then the two numbers are divided by $3$, hence if $(x,y,z)$ is a solution then $x=3a,y=3b$. I have $3a^2+3b^2=z^2$ and that implies $$ \left(\frac{a}{z}\right)^2+\left(\frac{b}{z}\right)^2=\frac{1}{3} $$ so I need to find the rational solutions of the equation $u^2+v^2=\frac{1}{3}$ and I think that there are no solutions for that because $\frac{1}{3}$ doesn't have a rational root, but I dont know how to explain it.  Thanks Proof of when is $A=X^TX$ invertible? Does $1.0000000000\cdots 1$ with an infinite number of $0$ in it exist? Ordering by the order of values in a SQL IN() clause