/ .NET

4. Doing Thread Scheduling and Priority the Right Way (Multithreading Fundamentals)

The series so far:

  1. The Unconventional Guide to Introduction to Thread
  2. Why Do People Think Dedicated CLR Threads is a Good Idea?
  3. How Not Knowing Thread Members and Execution State Makes You a Rookie
  4. Doing Thread Scheduling and Priority the Right Way
  5. How to Start Using CLR's Thread Pool
  6. What Wikipedia Can't Tell You About Thread Execution Contexts
  7. The Insider's Guide to Cooperative Cancellation and Timeout

Foreground Threads and Background Threads

The IsBackground property can be used to identify/change the thread’s background status.

Parameters Foreground Threads
Keep the application alive Foreground threads keep the application alive till a single foreground thread is active
Finally blocks Always execute on Foreground Thread
Ideal Ending Wait out for all background threads with timeout
Parameters Background Threads
Keep the application alive Background threads depends on Foreground Threads to keep the application alive
and terminates abruptly with Foreground Threads
Finally blocks Needs particular attention in cases of abrupt termination
Ideal Ending Wait out for all background threads with timeout

NOTE: The foreground/background status has no relation to the thread priority, and we observe Task Manager force stop all threads instantaneously - foreground and background.

Demo with Background and Foreground Threads

//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Threading;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Thread waterThread = new Thread(new ThreadStart(WaterSoundMethod));
            waterThread.IsBackground = true;

            Thread beeThread = new Thread(new ThreadStart(BeeSoundMethod));
            beeThread.IsBackground = true;

            
            waterThread.Start();
            Console.WriteLine("The thread's background status is: "+waterThread.IsBackground.ToString());
            beeThread.Start();
            Console.WriteLine("The thread's background status is: "+beeThread.IsBackground.ToString());
            
            Thread.Sleep(20);
            Console.WriteLine("Enjoying the nature");
            waterThread.Interrupt();
            waterThread.Join();
        }
        
        static void WaterSoundMethod()
        {
            try{
            while(true)
            {
                Console.WriteLine("Ffffffffff!");
                Thread.Sleep(10);
            }
            }
            catch(ThreadInterruptedException exception){
                Console.WriteLine(exception);
            }
        }
        
        static void BeeSoundMethod()
        {
            while(true)
            {
                Console.WriteLine("Buzzzzzzz!");
                Thread.Sleep(10);
            }
        }
    }
}

Thread Scheduling and Priorities

We should keep in mind below points for setting priority

  1. Lower Thread Priority for long-running tasks
  2. Raise Thread Priority for quick response short running tasks
How Thread Priority Class Maps to Process Priority?
Thread Priority Process Priority
THREAD_PRIORITY_CLASS IDLE_
PRIORITY_
CLASS
BELOW_
NORMAL_
PRIORITY_
CLASS
NORMAL_
PRIORITY_
CLASS
ABOVE_
NORMAL_
PRIORITY_
CLASS
HIGH_
PRIORITY_
CLASS
REALTIME_
PRIORITY_
CLASS
THREAD_PRIORITY_IDLE 1 1 1 1 1 16
THREAD_PRIORITY_LOWEST 2 4 6 8 11 22
THREAD_PRIORITY_BELOW_NORMAL 3 5 7 9 12 23
THREAD_PRIORITY_NORMAL 4 6 8 10 13 25
THREAD_PRIORITY_ABOVE_NORMAL 5 7 9 11 14 25
THREAD_PRIORITY_HIGHEST 6 8 10 12 15 26
THREAD_PRIORITY_TIME_CRITICAL 15 15 15 15 15 31

Exercise

Display 3 questions that user has to answer in 15 seconds (5 seconds to each). Display the score after 15 seconds.

Hints: Use the concept covered so far Thread States, Foreground and Background Threads

4. Doing Thread Scheduling and Priority the Right Way (Multithreading Fundamentals)
Share this

Subscribe to Coding Today

Popular queries