ConcurrentLinkedQueue does'n work as expected
<p>I'm developing an Andoid App that is made of a Service running in Background and some Activities connected to that Service. The Service runs on it's own Process.</p>

<p>My Service mainly has 3 classes: ServiceMain, ServiceWorker,Message.</p>

<p>ServiceMain has all the functions that are used by the Activities like logIn,logOut,send ... and so on.</p>

<p>Message represents a message that is sent to our server or recieved. Simply a String and a bool where the String is the message and the bool is a flag saying if a response from server is needed.</p>

<p>ServiceWorker is a subclass of Thread and does all the sending an recieving of messages using Sockets.</p>

<p>ServiceMain contains 2 Queues:</p>

<pre><code>Queue&lt;Message&gt; Sendingqueue= new ConcurrentLinkedQueue&lt;Message&gt;(); 
Queue&lt;Message&gt; Recievequeue = new ConcurrentLinkedQueue&lt;Message&gt;();
</code></pre>

<p>If the logIn method is called a ServiceWorker is created and started. In it's constructor it gets references to both queues and holds them.</p>

<pre><code>private final Queue&lt;Message&gt; Sendingqueue;
private final Queue&lt;Message&gt; Recievequeue; 
</code></pre>

<p>ServiceMain then creates some messages (M1,M2 for example) and adds them to the Sendingqueue.</p>

<p>The ServiceWorker builds the connection to our server and then runs into a loop where it looks for messages in Sendingqueue, sends them and doing some othe stuff like recieving ....</p>

<p>Hope the scenario is clear now.</p>

<p>Within ServiceWorker something strange happens on Sendingqueue:</p>

<p>Let's say ServiceMain added two messages, M1 and M2 to Sendingqueue while ServiceWorker is doing something time consuming or is not connected to our server. 
Sendingqueue now contains two messages.</p>

<p>If the ServiceWorker next time gets the length of the Queue it sees 2 items. Ok so far.
Then it calls peek() (the message is removed only if it was successfull sent) on the Sendingqueue and <strong>should</strong> get M1 because it was added first.
But it gets M2. 
The Sendingqueue seems to be reverted.</p>

<p>What's going wrong here ? What can I do to avoid this?</p>

<p>Thanks for any constructive reply.</p>

<p>Detlef</p>
