C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SettingThreadPriority
{
public class ABC
{
public void A()
{
Console.WriteLine("Thread A");
Console.WriteLine("Priority of Thread A is:" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
Thread.Sleep(5000);
Console.WriteLine("Thread with " + Thread.CurrentThread.Priority.ToString() + " has run");
}
public void B()
{
Console.WriteLine("Thread B");
Console.WriteLine("Priority of Thread B is:" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
Thread.Sleep(5000);
Console.WriteLine("Thread with " + Thread.CurrentThread.Priority.ToString() + " has run");
}
public static void C()
{
Console.WriteLine("Thread C");
Console.WriteLine("Priority of Thread C is:" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
Thread.Sleep(5000);
Console.WriteLine("Thread with " + Thread.CurrentThread.Priority.ToString() + " has run");
}
}
public class ScheduleExp
{
public static int Main(string[] args)
{
Console.WriteLine("..........Scheduling Threads");
Console.WriteLine(" ");
ABC a1 = new ABC();
Thread t1 = new Thread(new ThreadStart(a1.A));
Thread t2 = new Thread(new ThreadStart(a1.B));
Thread t3 = new Thread(new ThreadStart(ABC.C));
Console.WriteLine("Priority of Main Method is :" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
t1.Name = " Thread A";
t2.Name = " Thread B";
t3.Name = " Thread C";
t1.Priority = ThreadPriority.AboveNormal;
t2.Priority = ThreadPriority.BelowNormal;
t3.Priority = ThreadPriority.Lowest;
t1.Start();
t2.Start();
t3.Start();
Console.ReadKey();
return 0;
}
}
}
Output:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SettingThreadPriority
{
public class ABC
{
public void A()
{
Console.WriteLine("Thread A");
Console.WriteLine("Priority of Thread A is:" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
Thread.Sleep(5000);
Console.WriteLine("Thread with " + Thread.CurrentThread.Priority.ToString() + " has run");
}
public void B()
{
Console.WriteLine("Thread B");
Console.WriteLine("Priority of Thread B is:" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
Thread.Sleep(5000);
Console.WriteLine("Thread with " + Thread.CurrentThread.Priority.ToString() + " has run");
}
public static void C()
{
Console.WriteLine("Thread C");
Console.WriteLine("Priority of Thread C is:" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
Thread.Sleep(5000);
Console.WriteLine("Thread with " + Thread.CurrentThread.Priority.ToString() + " has run");
}
}
public class ScheduleExp
{
public static int Main(string[] args)
{
Console.WriteLine("..........Scheduling Threads");
Console.WriteLine(" ");
ABC a1 = new ABC();
Thread t1 = new Thread(new ThreadStart(a1.A));
Thread t2 = new Thread(new ThreadStart(a1.B));
Thread t3 = new Thread(new ThreadStart(ABC.C));
Console.WriteLine("Priority of Main Method is :" + Thread.CurrentThread.Priority.ToString());
Console.WriteLine(" ");
t1.Name = " Thread A";
t2.Name = " Thread B";
t3.Name = " Thread C";
t1.Priority = ThreadPriority.AboveNormal;
t2.Priority = ThreadPriority.BelowNormal;
t3.Priority = ThreadPriority.Lowest;
t1.Start();
t2.Start();
t3.Start();
Console.ReadKey();
return 0;
}
}
}
Output:
No comments:
Post a Comment