I have two tasks Job1 and Job2 and below is the code. First time I ran Job1 and Job2 in a sequence and got the stopwatch output as 844 milliseconds. Second time I commented sequential jobs and called parallel task processing and got the stopwatch result as 11352 milliseconds. My assumption/expectation was I should get the output as 422 (because 844/2) or somewhere near to it. But the result is totally opposite. I need to wait both the jobs to be completed and hence I put t1.Wait() and t2.Wait(); Please help me to process these two jobs in half of sequential processing. Please find my code below:
public ActionResult Index()
{
Stopwatch s = new Stopwatch();
s.Start();
//Execute 2 jobs in a sequence
Job1();
Job2();
//Execute 2 jobs in parallel
//var t1 = Task.Run(() => { Job1(); });
//var t2 = Task.Run(() => { Job2(); });
//t1.Wait();
//t2.Wait();
s.Stop();
Debug.WriteLine("ElapsedMilliseconds: " + s.ElapsedMilliseconds);
return View();
}
private void Job1()
{
for (int i = 0; i < 1000; i++)
{
Debug.WriteLine(i);
}
}
private void Job2()
{
for (int i = 2000; i < 3000; i++)
{
Debug.WriteLine(i);
}
}