jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Threads – Background and Foreground

With threads, as with most things in life, some are more important than others. Some threads are more important. Others are less important. That is where background and foreground threads come into play. When a program is alive, it 'always' waits for all threads to finish. That is obvious because you wrote a program so it does what you ask it do.  

However, a complicated system is always doing something. Think of your browser for instance. There was a time when browsers loaded web pages and nothing else. Now, browsers are so powerful, you can get almost everything done in them. For instance, I am writing this blog post in a browser. Format it in a browser and blog it in the browser. Heck, I wrote an entire novel and published it, all on a browser.  

When a developers develops something as complicated as a browser. Some day, you will too. When the time comes to quit an application, you want it to happen fast. If an application takes a long time to quit, that would be poor engineering. That is where background and foreground threads come into the picture.  

As far as the application is concerned, the application will always wait for its foreground processes to complete. The same won't happen with background threads. That means, suppose there are currently 10 background processes active. Then, 1 foreground process is active. Depending on the program flow, the CPU will do the whole multi-threading thing constantly. However, the moment the foreground thread reaches its logical end, the application will not wait. It ends, irrespective of whether or not the background processes have finished.  

As always, find code at our repo. 

Comments are closed