title: How to solve the producer-consumer using semaphores?
body: <p>I need to code a problem similar to producer-consumer, that must use semaphores. I tried a couple of solutions and none of them worked. First I tried a solution on Wikipedia and it didn't worked. My current code is something like that:</p>
  
  <p>Method run of the consumer:</p>
  
  <pre><code>    public void run() {
      int i=0;
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      String s = new String();
      while (1!=2){
          Date datainicio = new Date();
          String inicio=dateFormat.format(datainicio);
          try {
              Thread.sleep(1000);///10000
          } catch (InterruptedException e) {
              System.out.println("Excecao InterruptedException lancada.");
          }
          //this.encheBuffer.down();
          this.mutex.down();
          // RC
          i=0;
          while (i&lt;buffer.length) {
              if (buffer[i] == null) {
                  i++;
              } else {
                  break;
              }
          }
          if (i&lt;buffer.length) {
              QuantidadeBuffer.quantidade--;
              Date datafim = new Date();
              String fim=dateFormat.format(datafim);
              int identificador;
              identificador=buffer[i].getIdentificador()[0];
              s="Consumidor Thread: "+Thread.currentThread()+" Pedido: "+identificador+" Inicio: "+inicio+" Fim: "+fim+" posicao "+i;
              //System.out.println("Consumidor Thread: "+Thread.currentThread()+" Pedido: "+identificador+" Inicio: "+inicio+" Fim: "+fim+" posicao "+i);
              buffer[i]= null;
          }
          // RC
          this.mutex.up();
          //this.esvaziaBuffer.up();
          System.out.println(s);
    //            lock.up();
      }
  }
  </code></pre>
  
  <p>Method run of the producer:</p>
  
  <pre><code>    public void run() {
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      int i=0;
      while (1!=2){
          Date datainicio = new Date();
          String inicio=dateFormat.format(datainicio);
          // Produz Item
          try {
              Thread.sleep(500);//50000
          } catch (InterruptedException e) {
              System.out.println("Excecao InterruptedException lancada.");
          }
          //this.esvaziaBuffer.down();
          this.mutex.down();
          // RC
          i=0;
          while (i&lt;buffer.length) {
              if (buffer[i]!=null) {
                  i++;
              } else {
                  break;
              }
          }
          if (i&lt;buffer.length) {
              int identificador[]=new int[Pedido.getTamanho_identificador()];
              identificador[0]=i;
              buffer[i]=new Pedido();
              Produtor.buffer[i].setIdentificador(identificador);
              Produtor.buffer[i].setTexto("pacote de dados");
              QuantidadeBuffer.quantidade++;
              Date datafim = new Date();
              String fim=dateFormat.format(datafim);
              System.out.println("Produtor Thread: "+Thread.currentThread()+" Pedido: "+identificador[0]+" Inicio: "+inicio+" Fim: "+fim+" posicao "+i);
              i++;
          }
          // RC
          this.mutex.up();
          //this.encheBuffer.up();
      }
      //this.encheBuffer.up();
  }
  </code></pre>
  
  <p>In the above code it happened of a consumer thread to read a position and then, another thread read the same position without a producer fill that position, something like this:</p>
  
  <pre><code>Consumidor Thread: Thread[Thread-17,5,main] Pedido: 1 Inicio: 2011/11/27 17:23:33 Fim: 2011/11/27 17:23:34 posicao 1
  Consumidor Thread: Thread[Thread-19,5,main] Pedido: 1 Inicio: 2011/11/27 17:23:33 Fim: 2011/11/27 17:23:34 posicao 1
  </code></pre>