title: Java (Android) - Synchronized Queue and sending data
body: <p>I've two threads. One for generating data, second for sending them to server. Is this a classic producer-consumer situation?
  To do this I've constructed simple code for managing synchronised queue - I hope: I did it more or less correct? Could somebody answer me, please? My code is here below:</p>
  
  <pre><code>public ArrayList&lt;String&gt; Packets;
  
  public synchronized void add_to_Queue (String data) {
      Packets.add(data);
  }
  
  public synchronized void del_from_Queue (int position) {
      Packets.remove(position);
  }
  
  public synchronized String read_from_ Queue(int position) {
      return Packets.get(position);
  }
  
  public synchronized int number_of_element_of_Queue() {
      return Packets.size();
  }
  </code></pre>
  
  <p>First thread add new data by putting them using simple command:</p>
  
  <pre><code>add_to_Queue("XYZ);
  </code></pre>
  
  <p>Second one sending data in a loop:</p>
  
  <pre><code>while (OK)
  {
      try
      {
          while (number_of_element_of_Queue()&gt;0)
          {
              out.write(read_from_Queue(0)+"\n");
              out.flush;
              del_from_Queue(0); // if no error delete just sent element
          }
      }
      catch (IOException e1)
      {
        reconnect();
      }
  }
  </code></pre>
  
  <p>I think something is wrong because sending static data (simple static text instead of reading it from my "Queue") doesn't result in reconnection (i.e. after catch (IOException e1) ).  But when I use presented code, it happens very often, especially after reconnection. It does it several times (send some data, reconnect, send some more data, again reconnect and so on).</p>