At the moment, there is a fundamental flaw in the examples...
Look e.g. at https://github.com/Petikoch/Java_MVVM_with_Swing_and_RxJava_Examples/blob/master/src/main/java/ch/petikoch/examples/mvvm_rxjava/example2/Example_2_ViewModel.java
Every
@Override
public void connectTo(final Example_2_Model model) {
onEventFrom(vm2m_nameFirstname).execute(model::createAccount);
}
is basically just a typical RxJava
@Override
public void connectTo(final Example_2_Model model) {
Subscription sub1 = vm2m_nameFirstname.observeOn(Schedulers.io()).subscribe(model::createAccount);
}
This immediately assigns 1 thread from the CachedThreadScheduler (Schedulers.io()). The 1 thread stays assigned until "someone" would call sub1.unsubscribe().
But nobody every calls sub1.unsubscribe() here in these examples. Even if the Example_2_ViewModel instance is not used anymore and subject to garbage collection, the assigned 1 thread stays.
Therefor exists two fundamental issues in the examples:
-
Waste of resources: Imagine a new item "only" every 10 seconds in the vm2m_nameFirstname subject. It doesn't make sense to keep 1 real thread for all the time here "ready". Imagine you have in total thousands of subjects in your hundreds of viewmodels. For every observeOn(Schedulers.io()) you pay 1 thread. Therefor you'll have thousands of threads. It would be better to just submit something like a Runnable on every arriving item to something like a cached thread pool. Do we see here the price of the "Rx is single threaded by default" design?
-
Thread leaks: If the Example_2_ViewModel instance is dereferenced and subject to garbage collection, nobody does the unsubscribe and "releases" the assigned thread(s).
At the moment, there is a fundamental flaw in the examples...
Look e.g. at https://github.com/Petikoch/Java_MVVM_with_Swing_and_RxJava_Examples/blob/master/src/main/java/ch/petikoch/examples/mvvm_rxjava/example2/Example_2_ViewModel.java
Every
is basically just a typical RxJava
This immediately assigns 1 thread from the
CachedThreadScheduler(Schedulers.io()). The 1 thread stays assigned until "someone" would callsub1.unsubscribe().But nobody every calls
sub1.unsubscribe()here in these examples. Even if theExample_2_ViewModelinstance is not used anymore and subject to garbage collection, the assigned 1 thread stays.Therefor exists two fundamental issues in the examples:
Waste of resources: Imagine a new item "only" every 10 seconds in the
vm2m_nameFirstnamesubject. It doesn't make sense to keep 1 real thread for all the time here "ready". Imagine you have in total thousands of subjects in your hundreds of viewmodels. For everyobserveOn(Schedulers.io())you pay 1 thread. Therefor you'll have thousands of threads. It would be better to just submit something like aRunnableon every arriving item to something like a cached thread pool. Do we see here the price of the "Rx is single threaded by default" design?Thread leaks: If the
Example_2_ViewModelinstance is dereferenced and subject to garbage collection, nobody does the unsubscribe and "releases" the assigned thread(s).