title: Solving consumer producer concurrency issue with SynchronousQueue. Fairness property not working
body: <p>I am having a issue debugging my SynchronousQueue. its in android studio but should not matter its java code.  I am passing in true to the constructor of SynchronousQueue so its "fair" meaning its a fifo queue.  But its not obeying the rules, its still letting the consumer print first and the producer after. The second issue i have is i want these threads to never die, do you think i should use a while loop on the producer and the consumer thread and let them keep "producing and consuming" each other ?</p>
  
  <p>here is my simple code:</p>
  
  <pre><code>   package com.example.android.floatingactionbuttonbasic;
  
  
  import java.util.concurrent.SynchronousQueue;
  
  import trikita.log.Log;
  
  
  public class SynchronousQueueDemo {
  
  
      public SynchronousQueueDemo() {
      }
  
  
      public void startDemo() {
          final SynchronousQueue&lt;String&gt; queue = new SynchronousQueue&lt;String&gt;(true);
  
          Thread producer = new Thread("PRODUCER") {
              public void run() {
                  String event = "FOUR";
                  try {
                      queue.put(event); // thread will block here
                     Log.v("myapp","published event:", Thread
                             .currentThread().getName(), event);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
  
              }
          };
  
          producer.start(); // starting publisher thread
  
          Thread consumer = new Thread("CONSUMER") {
              public void run() {
                  try {
                      String event = queue.take(); // thread will block here
                      Log.v("myapp","consumed event:", Thread
                              .currentThread().getName(), event);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
  
              }
          };
  
          consumer.start(); // starting consumer thread
  
      }
  
  }
  </code></pre>
  
  <p>to start the threads i simple call  <code>new SynchronousQueueDemo().startDemo();</code></p>
  
  <p>The logs always look like this no matter what i pass to synchronousQueue constructor to be "fair":</p>
  
  <pre><code>/SynchronousQueueDemo$2$override(26747): myapp  consumed event: CONSUMER    FOUR
  V/SynchronousQueueDemo$1$override(26747): myapp published event:PRODUCER    FOUR
  </code></pre>
  
  <p>Checking the <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/SynchronousQueue.html" rel="nofollow">docs here</a>, it says the following:</p>
  
  <blockquote>
    <p>public SynchronousQueue(boolean fair)
    Creates a SynchronousQueue with the specified fairness policy.
    Parameters:
    fair - if true, waiting threads contend in FIFO order for access; otherwise the order is unspecified.</p>
  </blockquote>