Leveraging background threads
In Chapter 1, we learned how to create background threads and discussed some of their uses. Background threads have a lower priority than the primary thread of the process and other thread pool threads. In addition, active background threads will not prevent the user or the system from terminating the application.
This means that background threads are perfect for tasks such as the following:
- Writing log and analytics data
- Monitoring network or filesystem resources
- Reading data into the application
Do not use background threads for critical application operations such as the following:
- Saving application state
- Performing database transactions
- Application data processing
A good rule to follow when deciding whether some work can be processed by a background thread is to ask yourself whether abruptly interrupting the work to close the application would risk the data integrity of the system. So, how do you know...