title: Circular Buffer with Threads Consumer and Producer: it get stucks some executions
body: <p>I'm developing a circular buffer with two Threads: Consumer and Producer.
  I'm using active waiting with <code>Thread.yield</code>.
  I know that it is possible to do that with semaphores, but I wanted the buffer without semaphores.</p>
  
  <p>Both have a shared variable: <code>bufferCircular</code>.</p>
  
  <p>While the buffer is not full of useful information, <code>producer</code> write data in the position <code>p</code>of array, and while there are some useful information <code>consumer</code> read data in the position <code>c</code> of array. The variable <code>nElem</code> from <code>BufferCircular</code> is the number of value datas that haven't been read yet.</p>
  
  <p>The program works quite good 9/10 times that runs. Then, sometimes, it get stucks in a infinite loop before show the last element on screen (number 500 of loop for), or just dont' show any element.</p>
  
  <p>I think is probably a liveLock, but I can't find the mistake.</p>
  
  <p><strong>Shared Variable</strong>:</p>
  
  <pre><code>public class BufferCircular {
      volatile int[] array;
      volatile int p;
      volatile int c;
      volatile int nElem;
  
      public BufferCircular(int[] array) {
         this.array = array;
         this.p = 0;
         this.c = 0;
         this.nElem = 0;
      }
  
      public void writeData (int data) {
         this.array[p] = data;
         this.p = (p + 1) % array.length;
         this.nElem++;
      }
  
      public int readData() {
         int data = array[c];
         this.c = (c + 1) % array.length;
         this.nElem--;
         return data;
      }
  
  }
  </code></pre>
  
  <p><strong>Producer Thread</strong>:</p>
  
  <pre><code>public class Producer extends Thread {
      BufferCircular buffer;
      int bufferTam;
      int contData;
  
      public Productor(BufferCircular buff) {
         this.buffer = buff;
         this.bufferTam = buffer.array.length;
         this.contData = 0;
  
      }
  
      public void produceData() {
         this.contData++;
         this.buffer.writeData(contData);
      }
  
      public void run() {
         for (int i = 0; i &lt; 500; i++) {
            while (this.buffer.nElem == this.bufferTam) {
            Thread.yield();
          }
            this.produceData();
         }
      }
  }
  </code></pre>
  
  <p><strong>Consumer Thread</strong>:</p>
  
  <pre><code>    public class Consumer extends Thread {
          BufferCircular buffer;
          int cont;
  
          public Consumer(BufferCircular buff) {
             this.buffer = buff;
             this.cont = 0;
          }
  
          public void consumeData() {
             int data = buffer.readData();
             cont++;
             System.out.println("data  " + cont + ": " + data);
          }
  
          public void run() {
             for (int i = 0; i &lt; 500; i++) {
                while (this.buffer.nElem == 0) {
                   Thread.yield();
                }
                 this.consumeData();
             }
          }
     }
  </code></pre>
  
  <p><strong>Main</strong>:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
         Random ran = new Random();
         int tamArray = ran.nextInt(21) + 1;
         int[] array = new int[tamArray];
  
         BufferCircular buffer = new BufferCircular(array);
  
         Producer producer = new Producer (buffer);
         Consumer consumer = new Consumer (buffer);
  
         producer.start();
         consumer.start();
  
         try {
             producer.join();
             consumer.join();
         } catch (InterruptedException e) {
             System.err.println("Error with Threads");
             e.printStackTrace();
      }
  
      }
  
  }
  </code></pre>
  
  <p>Any help will be welcome.</p>