RxJava - main kills observable threads












0















Why main thread is killing my rxJava thread?



public static void main(final String args) throws Exception {
Observable.just(10)
.subscribeOn(Schedulers.newThread())
.subscribe(i -> print(i));
Thread.sleep(100);
}

private static void print(final int i) {
try {
Thread.sleep(5000);
} catch(final InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}


print method is blocking the thread for 5000 millis and I thought that JVM is waiting for all threads under application to be terminated.
In this case after Thread.sleep(100) is executed program shut down and I don't see 10 in a console.



Note: If I will use custom Executor like Executors.newFixedThreadPool(1); it will wait until shutdown, but with Schedulers.newThread() it won't.










share|improve this question





























    0















    Why main thread is killing my rxJava thread?



    public static void main(final String args) throws Exception {
    Observable.just(10)
    .subscribeOn(Schedulers.newThread())
    .subscribe(i -> print(i));
    Thread.sleep(100);
    }

    private static void print(final int i) {
    try {
    Thread.sleep(5000);
    } catch(final InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(i);
    }


    print method is blocking the thread for 5000 millis and I thought that JVM is waiting for all threads under application to be terminated.
    In this case after Thread.sleep(100) is executed program shut down and I don't see 10 in a console.



    Note: If I will use custom Executor like Executors.newFixedThreadPool(1); it will wait until shutdown, but with Schedulers.newThread() it won't.










    share|improve this question



























      0












      0








      0








      Why main thread is killing my rxJava thread?



      public static void main(final String args) throws Exception {
      Observable.just(10)
      .subscribeOn(Schedulers.newThread())
      .subscribe(i -> print(i));
      Thread.sleep(100);
      }

      private static void print(final int i) {
      try {
      Thread.sleep(5000);
      } catch(final InterruptedException e) {
      e.printStackTrace();
      }
      System.out.println(i);
      }


      print method is blocking the thread for 5000 millis and I thought that JVM is waiting for all threads under application to be terminated.
      In this case after Thread.sleep(100) is executed program shut down and I don't see 10 in a console.



      Note: If I will use custom Executor like Executors.newFixedThreadPool(1); it will wait until shutdown, but with Schedulers.newThread() it won't.










      share|improve this question
















      Why main thread is killing my rxJava thread?



      public static void main(final String args) throws Exception {
      Observable.just(10)
      .subscribeOn(Schedulers.newThread())
      .subscribe(i -> print(i));
      Thread.sleep(100);
      }

      private static void print(final int i) {
      try {
      Thread.sleep(5000);
      } catch(final InterruptedException e) {
      e.printStackTrace();
      }
      System.out.println(i);
      }


      print method is blocking the thread for 5000 millis and I thought that JVM is waiting for all threads under application to be terminated.
      In this case after Thread.sleep(100) is executed program shut down and I don't see 10 in a console.



      Note: If I will use custom Executor like Executors.newFixedThreadPool(1); it will wait until shutdown, but with Schedulers.newThread() it won't.







      java multithreading rx-java






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 10:08







      ByeBye

















      asked Nov 23 '18 at 10:00









      ByeByeByeBye

      3,8183941




      3,8183941
























          2 Answers
          2






          active

          oldest

          votes


















          4














          Schedulers.newThread() will act as a daemon thread: if main thread finishes - daemon thread won't stop JVM from shutdown. In this example this new (daemon) thread will go into print method and will be waiting for a 5 seconds, while main thread will be waiting just for a 0.1 sec and will just finish main method execution. That's all...






          share|improve this answer
























          • Thanks, it is an explanation.

            – ByeBye
            Nov 23 '18 at 10:40











          • You are always welcome :)

            – Andrey Ilyunin
            Nov 23 '18 at 10:41



















          0














          If not using ThreadPool you must wait (join) the created Thread yourself, see the join method; there are some overloading.



          You can get all explanation in the javadoc about ThreadPoolExecutor which offers you this kind of management.



          Let me know if you need further info.






          share|improve this answer


























          • How can I do it and why is it occurs? Can you edit your answer with that?

            – ByeBye
            Nov 23 '18 at 10:13











          • I just completed my answer.

            – Bsquare ℬℬ
            Nov 23 '18 at 10:38












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444432%2frxjava-main-kills-observable-threads%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Schedulers.newThread() will act as a daemon thread: if main thread finishes - daemon thread won't stop JVM from shutdown. In this example this new (daemon) thread will go into print method and will be waiting for a 5 seconds, while main thread will be waiting just for a 0.1 sec and will just finish main method execution. That's all...






          share|improve this answer
























          • Thanks, it is an explanation.

            – ByeBye
            Nov 23 '18 at 10:40











          • You are always welcome :)

            – Andrey Ilyunin
            Nov 23 '18 at 10:41
















          4














          Schedulers.newThread() will act as a daemon thread: if main thread finishes - daemon thread won't stop JVM from shutdown. In this example this new (daemon) thread will go into print method and will be waiting for a 5 seconds, while main thread will be waiting just for a 0.1 sec and will just finish main method execution. That's all...






          share|improve this answer
























          • Thanks, it is an explanation.

            – ByeBye
            Nov 23 '18 at 10:40











          • You are always welcome :)

            – Andrey Ilyunin
            Nov 23 '18 at 10:41














          4












          4








          4







          Schedulers.newThread() will act as a daemon thread: if main thread finishes - daemon thread won't stop JVM from shutdown. In this example this new (daemon) thread will go into print method and will be waiting for a 5 seconds, while main thread will be waiting just for a 0.1 sec and will just finish main method execution. That's all...






          share|improve this answer













          Schedulers.newThread() will act as a daemon thread: if main thread finishes - daemon thread won't stop JVM from shutdown. In this example this new (daemon) thread will go into print method and will be waiting for a 5 seconds, while main thread will be waiting just for a 0.1 sec and will just finish main method execution. That's all...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 10:17









          Andrey IlyuninAndrey Ilyunin

          1,256224




          1,256224













          • Thanks, it is an explanation.

            – ByeBye
            Nov 23 '18 at 10:40











          • You are always welcome :)

            – Andrey Ilyunin
            Nov 23 '18 at 10:41



















          • Thanks, it is an explanation.

            – ByeBye
            Nov 23 '18 at 10:40











          • You are always welcome :)

            – Andrey Ilyunin
            Nov 23 '18 at 10:41

















          Thanks, it is an explanation.

          – ByeBye
          Nov 23 '18 at 10:40





          Thanks, it is an explanation.

          – ByeBye
          Nov 23 '18 at 10:40













          You are always welcome :)

          – Andrey Ilyunin
          Nov 23 '18 at 10:41





          You are always welcome :)

          – Andrey Ilyunin
          Nov 23 '18 at 10:41













          0














          If not using ThreadPool you must wait (join) the created Thread yourself, see the join method; there are some overloading.



          You can get all explanation in the javadoc about ThreadPoolExecutor which offers you this kind of management.



          Let me know if you need further info.






          share|improve this answer


























          • How can I do it and why is it occurs? Can you edit your answer with that?

            – ByeBye
            Nov 23 '18 at 10:13











          • I just completed my answer.

            – Bsquare ℬℬ
            Nov 23 '18 at 10:38
















          0














          If not using ThreadPool you must wait (join) the created Thread yourself, see the join method; there are some overloading.



          You can get all explanation in the javadoc about ThreadPoolExecutor which offers you this kind of management.



          Let me know if you need further info.






          share|improve this answer


























          • How can I do it and why is it occurs? Can you edit your answer with that?

            – ByeBye
            Nov 23 '18 at 10:13











          • I just completed my answer.

            – Bsquare ℬℬ
            Nov 23 '18 at 10:38














          0












          0








          0







          If not using ThreadPool you must wait (join) the created Thread yourself, see the join method; there are some overloading.



          You can get all explanation in the javadoc about ThreadPoolExecutor which offers you this kind of management.



          Let me know if you need further info.






          share|improve this answer















          If not using ThreadPool you must wait (join) the created Thread yourself, see the join method; there are some overloading.



          You can get all explanation in the javadoc about ThreadPoolExecutor which offers you this kind of management.



          Let me know if you need further info.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 10:38

























          answered Nov 23 '18 at 10:11









          Bsquare ℬℬBsquare ℬℬ

          3,664101635




          3,664101635













          • How can I do it and why is it occurs? Can you edit your answer with that?

            – ByeBye
            Nov 23 '18 at 10:13











          • I just completed my answer.

            – Bsquare ℬℬ
            Nov 23 '18 at 10:38



















          • How can I do it and why is it occurs? Can you edit your answer with that?

            – ByeBye
            Nov 23 '18 at 10:13











          • I just completed my answer.

            – Bsquare ℬℬ
            Nov 23 '18 at 10:38

















          How can I do it and why is it occurs? Can you edit your answer with that?

          – ByeBye
          Nov 23 '18 at 10:13





          How can I do it and why is it occurs? Can you edit your answer with that?

          – ByeBye
          Nov 23 '18 at 10:13













          I just completed my answer.

          – Bsquare ℬℬ
          Nov 23 '18 at 10:38





          I just completed my answer.

          – Bsquare ℬℬ
          Nov 23 '18 at 10:38


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444432%2frxjava-main-kills-observable-threads%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?