Multithreading
Multithreading
using System;
class Program
{
// Function to calculate the sum of integers from 1 to 'n'
static int CalculateSum(int n)
{
int totalSum = 0;
for (int i = 1; i <= n; i++)
{
totalSum += i;
// Simulate some processing time (500 milliseconds)
System.Threading.Thread.Sleep(500); // Comment: This line introduces a delay of 500 milliseconds to
simulate processing time.
}
return totalSum;
}
Drawback of single
using System;
using System.Threading;
namespace MultiThreadingInCSharp
{
class ProgramP
{
static void Main(string[] args)
{
Method1();
Console.WriteLine("MEthod 1 execution is completed");
Method2();
Console.WriteLine("Method 2 execution is completed");
Method3();
Console.WriteLine("MEthod 3 execution is completed");
//Single thread
//Thread t =Thread.CurrentThread;
//t.Name = "Main Thread";
//Console.WriteLine($"Thread name {t.Name}");
//Console.WriteLine("My current thread-"+ Thread.CurrentThread.Name);
}
static void Method1()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 1 :" + i);
}
}
static void Method2()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 2 :" + i);
if (i == 2) { Console.WriteLine("executing code Started");
Thread.Sleep(10000);
Console.WriteLine("executing code Completed");
}
}
}
static void Method3()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 3 :" + i);
}
}
}
}Its using single thread its going line by line
Drawback of single
using System;
using System.Threading;
namespace MultiThreadingInCSharp
{
class ProgramP
{
static void Main(string[] args)
{
Method1();
Console.WriteLine("MEthod 1 execution is completed");
Method2();
Console.WriteLine("Method 2 execution is completed");
Method3();
Console.WriteLine("MEthod 3 execution is completed");
//Single thread
//Thread t =Thread.CurrentThread;
//t.Name = "Main Thread";
//Console.WriteLine($"Thread name {t.Name}");
//Console.WriteLine("My current thread-"+ Thread.CurrentThread.Name);
}
static void Method1()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 1 :" + i);
}
}
static void Method2()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 2 :" + i);
if (i == 2) { Console.WriteLine("executing code Started");
Thread.Sleep(10000);
Console.WriteLine("executing code Completed");
}
}
}
static void Method3()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 3 :" + i);
}
}
}
}Its using single thread its going line by line
***********************************MULTITHREAD*******************************************
*******
using System;
using System.Threading;
namespace MultiThreadingInCSharp
{
class ProgramP
{
static void Main(string[] args)
{
//Creating Threads
Thread t1 = new Thread(Method1)
{
Name = "Thread 1"
};
Thread t2 = new Thread(Method2);
t2.Name = "Thread 2";
}
}
namespace MultiThreadingInCSharp
{
class Program
{
static Semaphore _semaphore = new Semaphore(0, 2); // Create a semaphore with initial count 1
Console.ReadLine();
}
namespace MultiThreadingInCSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main method Started");
Console.WriteLine(Thread.CurrentThread.Name + "Thread Awake from sleep and trying to access the lock for ATM
ID: " + _toATM.ID);