0% found this document useful (0 votes)
19 views

Multi Threading Demo

File

Uploaded by

DJ Trap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Multi Threading Demo

File

Uploaded by

DJ Trap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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