I ran into a problem called parallel computing.
I have two classes, Load Balance and Printer.
The printer class contains lock to prevent another printer came across now the load balancer class has all printers it is his duty to manage printers to save time
namespace Printer
{
public class printer
{
public static string Name;
//constructor printer getting name of printer
public printer(string name) => Name = name;
public void print(int pages)
{
// this method is printing current requested page in given printer
lock (this)
{
Task .Run(() =>
{
for (int i = 0; i < pages; i++)
{
Console. WriteLine($"printing page {i} in printer {Name}");
Thread. Sleep(300);
}
});
}
}
}
public class Loadbalancer
{
// This class contains request came across main function and then it uses algorithm and decides to whom the request
// should be sent. This algorithm checks which printer has fewer pages
// so it should take less time
int page;
List<int> pages = new List<int>();
public Loadbalancer (int page)
{
int TotalPagesToP1 = 0;
int TotalPagesToP2 = 0;
// printer print = new printer(Printer);
printer p1 = new printer("HP 1222");
printer p2 = new printer("HP 1434");
pages.Add(page);
int currentIndex = 0;
for (; currentIndex < pages.Count; currentIndex++)
{
if(TotalPagesToP1 < TotalPagesToP2)
{
p1.print(pages[currentIndex]);
TotalPagesToP1 += pages[currentIndex];
}
else
{
p2.print(pages[currentIndex]);
TotalPagesToP2 += pages[currentIndex];
}
}
}
}
internal class Program
{
static void Main(string[] args)
{
// these are the page request to be printed which is sent to centralized load balancer class it
// it will decide to whom these page request should be sent
Task.Run(() =>
{
Loadbalancer k = new Loadbalancer(10);
Loadbalancer b = new Loadbalancer(20);
Loadbalancer c = new Loadbalancer(30);
});
Console.ReadKey();
}
}
}
The problem: printer now simulating only printer HP 434 is in action while HP101 is relaxed
I tried if else conditions
if ( printer 1 has less pages the printer 2 )
then give pages to printer 1
else printer 2
to synchronize