Performance/Fenix/Best Practices: Difference between revisions

(Create page with default dispatcher text)
 
Line 12: Line 12:
In general, <code>Dispatchers.Default</code> should only be used for computationally intensive tasks where tasks are FIFO priority or equal priority (this doesn't come up much in Android). It'd be much less misleading if it were named <code>Dispatchers.Computation</code>.
In general, <code>Dispatchers.Default</code> should only be used for computationally intensive tasks where tasks are FIFO priority or equal priority (this doesn't come up much in Android). It'd be much less misleading if it were named <code>Dispatchers.Computation</code>.


As for what to do if the default dispatcher is not a good fit, there's no one-size-fits-all solution. To choose the right threading scheme, it's important to understand your options: learn what each of the Dispatchers are intended for, what mechanisms they use to manage that, and their implications. Here are some additional considerations:
As for what to do if the default dispatcher is not a good fit, there's no one-size-fits-all solution. To choose the right threading strategy, it's important to understand your options: learn what each of the Dispatchers are intended for, what mechanisms they use to manage that, and their implications. Here are some additional considerations:
* <code>Dispatchers.IO</code> is often a much better default choice than the default dispatcher if your code does not produce many simultaneous tasks – which could generate many new threads – because it'll re-use existing threads.
* <code>Dispatchers.IO</code> is often a much better default choice than the default dispatcher if your code does not produce many simultaneous tasks – which could generate many new threads – because it'll re-use existing threads.
* Avoid creating a dedicated thread or thread pool unless it's strictly necessary: each new thread costs the system resources
* Avoid creating a dedicated thread or thread pool unless it's strictly necessary: each new thread costs the system resources


If you have questions about what the appropriate threading strategy is, please ask the perf team!
If you have questions about what the appropriate threading strategy is, please ask the perf team!
Confirmed users
975

edits