java - When using Schedulers, System.out.println prints nothing in RxJava -


i'm fiddling around rxjava , schedulers. implemented simple stream scheduler:

observable.just(1, 2, 3)       .doonnext(v -> thread.currentthread().getname())       .subscribeon(schedulers.newthread())       .subscribe(v -> system.out.println(v)); 

the example above prints nothing in console.

i noticed, when block main thread @ end using i.e. thread.sleep(), system.out.println prints proper values - 1 2 3:

observable.just(1, 2, 3)         .doonnext(v -> thread.currentthread().getname())         .subscribeon(schedulers.newthread())         .subscribe(v -> system.out.println(v));          try {             thread.sleep(10);         } catch (interruptedexception e) {             e.printstacktrace();         } 

can me understand behaviour?

rxjava uses daemon threads. app finishes before observable starts emission. it's quite easy check, pass scheduler returns non daemon thread , see output values:

scheduler scheduler = schedulers.from(executors.newcachedthreadpool(new threadfactory() {     @override public thread newthread(runnable r) {         thread result = new thread(r);         //result.setdaemon(true);         return result;     } }));  observable.just(1, 2, 3)         .subscribeon(scheduler)         .subscribe(v -> print(v)); 

uncomment line result.setdaemon(true); , values not printed.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -