Feb 28, 2020

THREAD PRIORITY IN MULTITHREADING


thread priority, thread priority in java, default priority of thread, thread priority example,
C# thread priority

C# THREAD PRIORITY 

“C# thread priority is the process of setting priority for the threads so that the can execute/perform their task as the priority specified is known as thread priority in multithreading. “
When we perform multithreading inc# then the CPU utilize its resources equally among all the thread and give equal time to execute that threads. Now the problem is if we have two different methods and that are executing by two different threads but the method one has to do more work than two in these case we can set the priority for the threads we can give specify more CPU resources to thread one to complete its work as compare to thread two, therefore we use thread priority in multithreading.
            We have five level of thread priority in multithreading
1.    Lowest
In this the level the selected thread consumes least CPU resources as compare to the all others to execute.
2.    Below Normal
After lowest this level will came. This level consume more CPU resources as compare to Lowest but less as compare to all others.
3.    Normal
The default priority of threads when we create threads then the CPU utilize its resources equally among the threads that is the Normal level.
4.    Above Normal
It consume more resources as compare to the above four but less resources as compare to highest level.
5.    Highest  
Last and powerful level in this level the selected thread consumes the highest resources of CPU as compare to the all others. It is used when a thread have to work more than all thread exist in program.

Syntax

            After instantiating child thread and specify its task, we can set the priority in such a way.
                [<Th>].Priority = ThreadPriority. <Level>
           Dot Priority = ThreadPriority. is the keyword in c# thread priority, where in place of <Th> the thread instance will come and in place of <Level> the five level which is detailed above will placed. If the syntax does work then try this one.
                [<Th>].Priority = System.Threading.ThreadPriority. <Level>;
Sometime the compiler does not recognize only ThreadPriority so don't vary about this error use the full syntax of System. Threading .ThreadPriority and then mention level

THREAD PRIORITY EXAMPLE

Note : Don’t forget to add System. Threading
using System.Threading;
namespace ThreadPriority
{
    class Program
    {
        public static void Test()
        {
            for (int a =1; a<=20; a++)
            {
                if (a%2==0)
                {
                    Console.WriteLine(+a + " is even number");
                }
                else
                {
                    Console.WriteLine(+a + " is odd number");
                }
            }
        }
        public static void Test2()
        {
            int a = 1;
            while(a<=50)
            {
                Console.WriteLine(a);
                a++;
            }
        }
        static void Main(string[] args)
        {
            Thread t1 = new Thread(Test);
            Thread t2 = new Thread(Test2);
            t1.Priority = System.Threading.ThreadPriority.Highest;
            t2.Priority = System.Threading.ThreadPriority.Lowest;
            t1.Start();
            t2.Start();
            Console.ReadKey();
        }
    }
}

Related Topics


0 comments:

Post a Comment

Please do not enter any spam link in the comment box