Sunday, December 28, 2014

Program to show the use of Thread Priority using C#

9:17 PM - By azeheruddin khan 0

Explanation: - In the below given code, the following statement: -

 t1.Priority = ThreadPriority.Highest;

Priority’ is a property of Thread class, which is used to get or set the priority value of a thread. ‘ThreadPriority’ is an enumeration which consists of some constant values that represent the priority of the thread they are: -
  1. Highest
  2. Above Normal
  3. Normal
  4. Below Normal
  5. Lowest

Example:



using System;
using System.Threading;

namespace ThreadPriorityExample
{
    class Program
    {
        static void Main(string[] args)
        {
            RailReservation r = new RailReservation();
            ThreadStart th1 = new ThreadStart(r.givdet);
            ThreadStart th2 = new ThreadStart(r.search);
            ThreadStart th3 = new ThreadStart(r.booking);
            Thread t1 = new Thread(th1);
            Thread t2 = new Thread(th2);
            Thread t3 = new Thread(th3);
            t1.Priority = ThreadPriority.Highest;
            t2.Priority = ThreadPriority.Normal;
            t3.Priority = ThreadPriority.Lowest;
            t1.Start();
            t2.Start();
            t3.Start();
            Console.ReadLine();

        }
    }


    class RailReservation
    {
        public void givdet()
        {
            lock (this)
                for (int i = 1; i <= 3; i++)
                {
                    Console.WriteLine("passenger" + i + " gives journey details");
                    Thread.Sleep(1000);
                }
            Console.WriteLine();
        }
        public void search()
        {
            lock (this)
                for (int i = 1; i <= 3; i++)
                {
                    Console.WriteLine("passenger" + i + " selects the train and type of reservation");
                    Thread.Sleep(1000);
                }
            Console.WriteLine();
        }
        public void booking()
        {
            lock (this)
                for (int i = 1; i <= 3; i++)
                {
                    Console.WriteLine("passenger" + i + " books the ticket");
                    Thread.Sleep(1000);
                }
        }
    }

}


Output:



About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top