You are on page 1of 2

using System;

using System.Threading;
namespace MultithreadingDemo01Kcmt
{
class Abc
{
public void Show()
{
for (char alph = 'a'; alph != 'z'; alph++)
{
Console.WriteLine(alph+"\t"+Thread.CurrentThread.Name);
Thread.Sleep(200);
}
}
public void Display()
{
for (int i = 0; i <= 26; i++)
{
Console.WriteLine(i + "\t" + Thread.CurrentThread.Name);
Thread.Sleep(200);
}
}
}
class Program
{
static void Main()
{
Abc obj1 = new Abc();
//1.0
//obj1.Show();
//obj1.Display();
//1.1
Thread t1 = new Thread(obj1.Show);
t1.Name = "First User defined thread";
Thread t2 = new Thread(obj1.Display);
t2.Name = "Second user defined thread";
t1.Start();
t2.Start();
Console.ReadKey();
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++
using System;
using System.Threading;
namespace MultithreadingDemo01Kcmt
{
class Abc
{
public void Show()
{
//lock (this)
//{
// for (char alph = 'a'; alph != 'z'; alph++)
// {
// Console.WriteLine(alph + "\t" + Thread.CurrentThread.Name);
// Thread.Sleep(200);
// }
//}
//or
Monitor.Enter(this);
for (char alph = 'a'; alph != 'z'; alph++)
{
Console.WriteLine(alph + "\t" + Thread.CurrentThread.Name);
Thread.Sleep(200);
}
Monitor.Exit(this);

}
class Program
{
static void Main()
{
Abc obj1 = new Abc();
Thread t1 = new Thread(obj1.Show);
t1.Name = "First User defined thread";
Thread t2 = new Thread(obj1.Show);
t2.Name = "Second user defined thread";
t1.Start();
t2.Start();
Console.ReadKey();
}
}
}

You might also like