Skip to content

Fundamental waste of threads plus potential thread leaks #1

@Petikoch

Description

@Petikoch

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:

  1. 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?

  2. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions