idisfkj/android-startup

Benefit of Topological Sort in Async Initialization

kbabcockuf opened this issue · 1 comments

Itssssss Rousetime!

Thanks for the great library! It's exactly what I'm looking for to perform multithreaded, eager initialization of app startup.

What is the benefit to the topological sort for a series of async dependencies? Assuming we have a set of N-number Android Startups, none of which require main thread, we'd end up with N latched Runnables. It seems it'd just be first-to-finish as each Runnable is unlatched and completed, and the order these Runnables are dispatched to an executor won't matter, assuming we have N-number threads available. I suppose if the underlying thread pool is limited to X-number threads, then the sort guarantees the order of Runnables getting access to the pool.

Any other insight here?

Thanks!

@kbabcockuf
Thank you for your insight

The purpose of topological sort is to target interdependent startup tasks. When the dependencies are complex, we can use topological sort to find the highest priority task to solve, so that other tasks can run quickly.

If there is an asynchronous dependency, an asynchronous task also needs to wait until the task on which it depends completes before running.

If all the tasks have no dependencies, then in theory, the effect of topological sorting will disappear. If they are synchronous, it will run automatically in the order in which the user added the tasks. If it's asynchronous, it starts multiple threads running in turn.