RxJava - main kills observable threads
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
add a comment |
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
add a comment |
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
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
java multithreading rx-java
edited Nov 23 '18 at 10:08
ByeBye
asked Nov 23 '18 at 10:00
ByeByeByeBye
3,8183941
3,8183941
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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...
Thanks, it is an explanation.
– ByeBye
Nov 23 '18 at 10:40
You are always welcome :)
– Andrey Ilyunin
Nov 23 '18 at 10:41
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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...
Thanks, it is an explanation.
– ByeBye
Nov 23 '18 at 10:40
You are always welcome :)
– Andrey Ilyunin
Nov 23 '18 at 10:41
add a comment |
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...
Thanks, it is an explanation.
– ByeBye
Nov 23 '18 at 10:40
You are always welcome :)
– Andrey Ilyunin
Nov 23 '18 at 10:41
add a comment |
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...
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...
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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