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

Multithreading

Uploaded by

sickboy.adi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Multithreading

Uploaded by

sickboy.adi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

SINGLE THREAD:

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;
}

static void Main()


{
int num = 10; // We want to calculate the sum of integers from 1 to 10
Console.WriteLine($"Calculating the sum of integers from 1 to {num}"); // Comment: Display the range of
integers to be summed.
int result = CalculateSum(num);
Console.WriteLine("The sum is: " + result); // Comment: Display the calculated sum.
}
}

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";

Thread t3 = new Thread(Method3)


{
Name = "Thread 3"
};
t1.Start();
t2.Start();
t3.Start();
Console.WriteLine("Main Thread Ended");
//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()
{
Console.WriteLine("Method 1 Started using "+ Thread.CurrentThread.Name);
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 1 :" + i);
}
Console.WriteLine("Method 1 Ended using-->" + Thread.CurrentThread.Name);
}
static void Method2()
{
Console.WriteLine("Method 2 Started using " + Thread.CurrentThread.Name);
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");
}
}
Console.WriteLine("Method 2 Ended using-->" + Thread.CurrentThread.Name);
}
static void Method3()
{
Console.WriteLine("Method 3 Started using " + Thread.CurrentThread.Name);
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Method 3 :" + i);
}
Console.WriteLine("Method 3 Ended using-->" + Thread.CurrentThread.Name);
}

}
}

Semaphore using System;


using System.Threading;

namespace MultiThreadingInCSharp
{
class Program
{
static Semaphore _semaphore = new Semaphore(0, 2); // Create a semaphore with initial count 1

static void Main(string[] args)


{
for (int i = 0; i < 5; i++)
{
new Thread(Write).Start();
}

Console.ReadLine();
}

public static void Write()


{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " - Write thread waiting");

_semaphore.WaitOne(); // Acquire the semaphore

Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " - Write Thread Working");


Thread.Sleep(5000); // Simulate some work being done
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " - Write Thread Finished");

_semaphore.Release(); // Release the semaphore


}
}
}
Deadlock using System;
using System.Threading;

namespace MultiThreadingInCSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main method Started");

// Create two ATMDetails objects with different IDs and balances


ATMDetails details1 = new ATMDetails(501, 5000);
ATMDetails details2 = new ATMDetails(502, 6000);
// Create two ATMSystem objects, specifying the source and destination ATMs, and the amount to transfer
ATMSystem system1 = new ATMSystem(details1, details2, 500);
Thread T1 = new Thread(system1.Transfer);
T1.Name = "T1";

ATMSystem system2 = new ATMSystem(details2, details1, 1000);


Thread T2 = new Thread(system2.Transfer);
T2.Name = "T2";

// Start the threads


T1.Start();
T2.Start();

// Wait for the threads to complete


T1.Join();
T2.Join();

Console.WriteLine("Main method Completed ");


}
}

public class ATMDetails


{
private double _balance;
private int _id;

public ATMDetails(int id, double balance)


{
this._id = id;
this._balance = balance;
}

public int ID { get { return _id; } }

public void Withdraw(double amount)


{
_balance -= amount;
}

public void Deposit(double amount)


{
_balance += amount;
}
}

public class ATMSystem


{
private ATMDetails _fromATM;
private ATMDetails _toATM;
private double _amountToTransfer;

public ATMSystem(ATMDetails fromATM, ATMDetails toATM, double amountToTransfer)


{
_fromATM = fromATM;
_toATM = toATM;
_amountToTransfer = amountToTransfer;
}

public void Transfer()


{
Console.WriteLine(Thread.CurrentThread.Name + "Thread Trying to access lock for ATM ID: " + _fromATM.ID);

// Acquire a lock on the source ATM (_fromATM)


lock (_fromATM)
{
Console.WriteLine(Thread.CurrentThread.Name + "Thread Acquired lock for ATM ID: " + _fromATM.ID);
Console.WriteLine(Thread.CurrentThread.Name + "Thread Trying to sleep for 1 second");
Thread.Sleep(1000);

Console.WriteLine(Thread.CurrentThread.Name + "Thread Awake from sleep and trying to access the lock for ATM
ID: " + _toATM.ID);

// Acquire a lock on the destination ATM (_toATM)


lock (_toATM)
{
// Perform the transfer: withdraw from the source ATM and deposit into the destination ATM
_fromATM.Withdraw(_amountToTransfer);
_toATM.Deposit(_amountToTransfer);
}

Console.WriteLine(Thread.CurrentThread.Name + "Thread Released locks");


}
}
}
}

You might also like