title: Concurrently Dequeueing an item
body: <p>Assume we have two threads working with a method that execute below:</p>
  
  <pre><code>while(true){
  
      if(Queue.Count() &lt;= 0){
          wait();
      }
  
      object anObject = Queue.Dequeue();
  }
  </code></pre>
  
  <p>Now the problem occurs when Queue has one element init, Thread 1 is about to execute Queue.Count line, Thread 2 about is on Queue.Dequeue() and execution priority is on Thread 1.</p>
  
  <p>As this situation occurs, Thread 1 will throw an exception because, Queue.Count() will return 1 and it will try to dequeue an object from an empty queue. How can I handle this? What is the best solution if I want to dequeue safely? Should I use syncronized or lock something? </p>
  
  <p>Best regards,
  Kemal</p>