0% found this document useful (0 votes)
7 views2 pages

Program

The document contains a C# program that demonstrates multithreading by calculating the sum of an array of numbers and performing division operations using two separate threads. It includes a class, MymathLib, which handles the division logic while ensuring thread safety with locks. The main method initializes and starts threads for both summation and division, then waits for their completion.

Uploaded by

chloekhoury2004
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)
7 views2 pages

Program

The document contains a C# program that demonstrates multithreading by calculating the sum of an array of numbers and performing division operations using two separate threads. It includes a class, MymathLib, which handles the division logic while ensuring thread safety with locks. The main method initializes and starts threads for both summation and division, then waits for their completion.

Uploaded by

chloekhoury2004
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

namespace _11_threadingtest

{
internal class Program
{
static int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , 11 , 12};
static int sum = 0;
static object lockObject = new object();
static int halfway = numbers.Length / 2;
static MymathLib mymathLibobj = new MymathLib();
static void Main(string[] args)
{

//Thread thread1 = new Thread((calculateSum));


//Thread thread2 = new Thread((calculateSum));
//thread1.Start(0);
//thread2.Start(halfway);
//thread1.Join();
//thread2.Join();
//Console.WriteLine(sum);
Thread thread3 = new Thread((mymathLibobj.Divide));
Thread thread4 = new Thread((mymathLibobj.Divide));
thread3.Start();
thread4.Start();
thread3.Join();
thread4.Join();

static void calculateSum(object Startindex) {


Console.WriteLine("some running the other method");
Console.WriteLine($"the other method thread
{Thread.CurrentThread.ManagedThreadId}");
Thread.Sleep(1000);
int startindex = (int)Startindex;
//int endindex = (int)Endindex;
for (int i = startindex; i < (halfway + (int)Startindex); i++) {
lock (lockObject) {
Thread.Sleep(1000);
Console.WriteLine($"the other method thread
{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine("i = " + i);
sum += numbers[i];
}
}
Console.WriteLine("Finished the other method");
Console.WriteLine($"the other method thread
{Thread.CurrentThread.ManagedThreadId}");
}
}

class MymathLib
{
public int Numb1;
public int Numb2;
Random randnumb = new Random();
static object lockObject = new object();
public MymathLib() {
Console.WriteLine("some running the other method");
Console.WriteLine($"the other method thread
{Thread.CurrentThread.ManagedThreadId}");
}
public void Divide() {
for(int i = 0; i < 100000; i++)
{
lock (lockObject)
{ //safe thread objects.
Numb1 = randnumb.Next(1,2);
Numb2 = randnumb.Next(1,2);

//other thread rest the values of numb1 and numb2


try { int division = Numb1 / Numb2;
//Thread.Sleep(1000);
//Console.WriteLine($"the other method thread
{Thread.CurrentThread.ManagedThreadId} division value {division}");
}
catch(Exception ex) {
Console.WriteLine($"the other method thread
{Thread.CurrentThread.ManagedThreadId} division value exception");
ex.GetType(); }
Numb1 = 0;
Numb2 = 0;
}
}
}
}
}

You might also like