-
Notifications
You must be signed in to change notification settings - Fork 30
Description
At the moment, there is a fundamental flaw in the examples...
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_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).