title: Java 9 - how publisher and subscriber works
body: <p>I am trying to understand how <code>Subscriber</code> and <code>Publisher</code> works in <strong>java 9</strong>.</p>
  
  <p>Here I have created one <code>subscriber</code> here and using <code>SubmissionPublisher</code> for publishing item .</p>
  
  <p>I am trying to publish 100 strings to <code>subscriber</code>. If I do not make the <code>Client</code> program to <code>sleep</code> (<strong>see commented code in <code>MyReactiveApp</code></strong>), I do not see all the items are published.</p>
  
  <p>why is it not waiting for all the strings processed here:</p>
  
  <pre><code>strs.stream().forEach(i -&gt; publisher.submit(i)); // what happens here? 
  </code></pre>
  
  <p>If I replace the above code with, I see all the strings are printed in console</p>
  
  <pre><code>strs.stream().forEach(System.out::println);
  </code></pre>
  
  <p>Client program that publishes using <code>SubmissionPublisher</code>. </p>
  
  <pre><code>import java.util.List;
  import java.util.concurrent.SubmissionPublisher;
  import java.util.function.Supplier;
  import java.util.stream.Collectors;
  import java.util.stream.Stream;
  
  public class MyReactiveApp {
  
      public static void main(String args[]) throws InterruptedException {
  
          SubmissionPublisher&lt;String&gt; publisher = new SubmissionPublisher&lt;&gt;();
  
          MySubscriber subs = new MySubscriber();
          publisher.subscribe(subs);
  
  
          List&lt;String&gt; strs = getStrs();
  
          System.out.println("Publishing Items to Subscriber");
          strs.stream().forEach(i -&gt; publisher.submit(i));
  
          /*while (strs.size() != subs.getCounter()) {
              Thread.sleep(10);
          }*/
  
          //publisher.close();
  
          System.out.println("Exiting the app");
  
      }
  
      private static List&lt;String&gt; getStrs(){
  
          return Stream.generate(new Supplier&lt;String&gt;() {
              int i =1;
              @Override
              public String get() {
                  return "name "+ (i++);
              }
          }).limit(100).collect(Collectors.toList());
      }
  
  }
  </code></pre>
  
  <p>Subscriber</p>
  
  <pre><code>import java.util.concurrent.Flow.Subscription;
  
  public class MySubscriber implements java.util.concurrent.Flow.Subscriber&lt;String&gt;{
  
      private Subscription subscription;
  
      private int counter = 0;
  
      @Override
      public void onSubscribe(Subscription subscription) {
          this.subscription = subscription;
          subscription.request(100);
  
      }
  
      @Override
      public void onNext(String item) {
          System.out.println(this.getClass().getSimpleName()+" item "+item);
          //subscription.request(1);
          counter++;
  
      }
  
      @Override
      public void onError(Throwable throwable) {
          System.out.println(this.getClass().getName()+ " an error occured "+throwable);
  
      }
  
      @Override
      public void onComplete() {
          System.out.println("activity completed");
  
      }
      public int getCounter() {
          return counter;
      }
  
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>Publishing Items to Subscriber
  MySubscriber item name 1
  MySubscriber item name 2
  MySubscriber item name 3
  MySubscriber item name 4
  MySubscriber item name 5
  Exiting the app
  MySubscriber item name 6
  MySubscriber item name 7
  MySubscriber item name 8
  MySubscriber item name 9
  MySubscriber item name 10
  MySubscriber item name 11
  MySubscriber item name 12
  </code></pre>