Unit7 C#
Unit7 C#
Thread Class
The thread class helps us to perform task such as creating and setting the
priority of thread. We can use this class to control a thread and obtain it’s status.
The thread class provide various properties that allow us to perform task such
as obtaining a status of thread and specifying a name for thread. Following table
shows the important method of thread class.
Method Task
Start To start thread
Sleep To block the current thread for a specified time period
Suspend To suspend a thread
Resume To resume a thread which has been suspend
Join To block a thread until another thread has terminated
Interrupt To interrupt the thread which is in the sleep,join,wait state
SpinWait To make a thread wait the no of time specified in iteration
parameter.
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
class demo
{
public void first()
{
Console.WriteLine("First method is run for Thread t1");
Thread.Sleep(500);
Console.WriteLine("First method is terminated");
}
public void second()
{
Console.WriteLine("second method is run for Thread t2");
Thread.Sleep(500);
Console.WriteLine("second method is terminated");
}
}
class Program
{
static void Main(string[] args)
{
demo d = new demo();
Thread t1 = new Thread(d.first);
Thread t2 = new Thread(d.second);
Console.WriteLine("Thread t1 is started");
t1.Start();
Console.WriteLine("Thread t2 is started");
t2.Start();
Console.ReadKey();
}
}
}
Thread Priorities
Each thread has a priority setting associated with it .A thread priority determine
impart how frequently a thread gain access to the CPU.In general low priority thread
gain access to the CPU less often than high priority thread. As a result within given
ThreadPriority.Highest;
ThreadPriority.AboveNormal;
ThreadPriority.Normal;
ThreadPriority.BelowNormal;
ThreadPriority.BelowNormal;
ThreadPriority.Lowest;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication3
{
class demo
{
public void read1()
{
Console.WriteLine("Thread t1 is started");
Console.WriteLine("Priority of t1 is " +
Thread.CurrentThread.Priority);
}
public void read2()
{
Console.WriteLine("Thread t2 is started");
Console.WriteLine("Priority of t2 is " +
Thread.CurrentThread.Priority);
}
public void read3()
class Program
{
static void Main(string[] args)
{
demo d = new demo();
Thread t1 = new Thread(d.read1);
Thread t2 = new Thread(d.read2);
Thread t3 = new Thread(d.read3);
Thread t4 = new Thread(d.read4);
Thread t5 = new Thread(d.read5);
t1.Priority = ThreadPriority.Lowest;
t2.Priority = ThreadPriority.BelowNormal;
t4.Priority = ThreadPriority.AboveNormal;
t5.Priority = ThreadPriority.Highest;
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
Console.ReadKey();
}
}
}
Synchronization
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication4
{
class demo
{
int x = 90, y = 10, sum;
private object lock_obj = new object();
public void lock_demo()
{
sum = 0;
Console.WriteLine(Thread.CurrentThread.Name + " is started");
lock(lock_obj )
{
sum = x + y;
}
Console.WriteLine("In thread " + Thread.CurrentThread.Name + "
Sum is" + sum);
Thread.Sleep(500);
Console.WriteLine(Thread.CurrentThread.Name + "Terminated");
}
}
class Program
{
static void Main(string[] args)
{
demo d = new demo();
t1.Start();
t2.Start();
t3.Start();
Console.ReadKey();
}
}
}