title: What is the relationship between Thread.sleep and happens-before?
body: <p>I've written a simple application, which has main thread(producer) and multiple consumer threads. I want to broadcast a message from main thread, so all the consumer threads will receive it. 
  However, I have troubles.
  I'm trying to understand how <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)" rel="nofollow noreferrer">Thread.sleep</a> may be related to <a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.5" rel="nofollow noreferrer">Happens-Before</a>. Here's my piece of code:</p>
  
  <pre><code>import java.util.*;
  
  public class PubSub {
  
      public static void main(String[] args) {
          List&lt;Consumer&gt; consumers = new ArrayList&lt;&gt;();
  
          for (int i = 0; i &lt; 3; i++ ) {
              Consumer consumer = new Consumer(
                      "Consumer"  + i
              );
              consumer.start();
  
              consumers.add(consumer);
          }
  
          Scanner scanner = new Scanner(System.in);
  
          while(true) {
              String message = scanner.nextLine();
  
              for (Consumer consumer: consumers) {
                  consumer.notify(message);
              }
  
          }
      }
  
      static class Consumer extends Thread {
  
          private Queue&lt;String&gt; queue;
  
          public Consumer(String name) {
              super(name);
              this.queue = new LinkedList&lt;&gt;();
          }
  
          @Override
          public void run() {
              while(true) {
  
                  if (!queue.isEmpty()) {
                      String message = queue.poll();
  
                      System.out.println(
                              getName() + ": Consuming message: " + message
                      );
                  }
              }
          }
  
          public void notify(String message) {
              queue.add(message);
          }
      }
  
  }
  </code></pre>
  
  <p>If I add sleep, the consumer will start receiving messages:</p>
  
  <pre><code>@Override
  public void run() {
      while(true) {
  
          try {
              Thread.sleep(0);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
          if (!queue.isEmpty()) {
              String message = queue.poll();
  
              System.out.println(
                      getName() + ": Consuming message: " + message
              );
          }
      }
  }
  </code></pre>
  
  <p>Since Thread.sleep is a native method, I want to understand how it resolves happens-before.</p>
  
  <p>I must note, that the true way of fixing happens-before is to make <strong>volatile</strong> keyword:</p>
  
  <pre><code>private volatile Queue&lt;String&gt; queue;
  </code></pre>
  
  <p>If you add <strong>synchronization</strong> onto the <strong>queue</strong>, it will also fix the issue:</p>
  
  <pre><code>@Override
  public void run() {
      synchronized (queue) {
          while (true) {
  
              if (!queue.isEmpty()) {
                  String message = queue.poll();
  
                  System.out.println(
                          getName() + ": Consuming message: " + message
                  );
              }
          }
      }
  }
  </code></pre>