-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make NewThreadScheduler create Daemon threads #631
Make NewThreadScheduler create Daemon threads #631
Conversation
This matches the behavior of Schedulers.COMPUTATION_EXECUTOR and Schedulers.IO_EXECUTOR. See https://groups.google.com/forum/#!topic/rxjava/Qe1qi0aHtnE and ReactiveX#431 (comment)
RxJava-pull-requests #561 FAILURE |
This solves the non-termination problem, but should those terminations be so harsh? Perhaps allowing the core threads to timeout might be a better alternative to all of these schedulers, although the tuning of timeout value could be of concern. |
I don't understand what you mean. Can you elaborate? For example, what timeout are you referring to? This code should not prevent the JVM from exiting: public static void main(String args[]) {
Observable.from(1, 2, 3, 4).observeOn(Schedulers.newThread()).toBlockingObservable().forEach(new Action1<Integer>() {
@Override
public void call(Integer i) {
System.out.println("i: " + i);
}
});
} It should be application logic that chooses to block, not the exit of a JVM. |
I was talking about the thread pool and core thread timeouts (aka keepalive time): private static class EventLoopScheduler extends Scheduler {
private final ExecutorService executor;
private EventLoopScheduler() {
ThreadPoolExecutor e = (ThreadPoolExecutor)Executors.newFixedThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "RxNewThreadScheduler-" + count.incrementAndGet());
}
});
e.setKeepAliveTime(1, TimeUnit.SECONDS); // <-- mabe lower, maybe greater time in general
e.allowCoreThreadTimeOut(true);
executor = e;
}
...
} The problem I encounter with daemon-threaded pools is that when the main thread exists, these threads stop immediately and don't finish their given tasks. This is why there are |
If something needs to be waited on then Sleep and timeouts are non-deterministic and incorrect for using to keep an application running. |
Fair enough. But there is still an issue with NewThreadScheduler. Even if quitting the application removes these threads, performing thousand scheduling operation will yield thousand threads or an exception about the inability to create more threads. So until the application quits, these threads stay. I suggest adding the e.setKeepAliveTime(50, TimeUnit.MILLISECONDS);
e.allowCoreThreadTimeOut(true); from my example to the NewThreadScheduler to be safe. 50 milliseconds might be to big since threads are no way to be reused, so decreasing it to a small number (zero is disallowed) is also possible. Test program:
|
- there is no guarantee for how many threads Interval will use so useless to assert anything on it
RxJava-pull-requests #562 SUCCESS |
Make NewThreadScheduler create Daemon threads
Should I open a new issue for the thread exhaustion problem? |
…ler-Daemon Make NewThreadScheduler create Daemon threads
This matches the behavior of Schedulers.COMPUTATION_EXECUTOR and Schedulers.IO_EXECUTOR.
See https://groups.google.com/forum/#!topic/rxjava/Qe1qi0aHtnE and #431 (comment)