using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ThreadNTask
{
class Program
{
static void Main(string[] args)
{
long from = Convert.ToInt64(args[0]);
long to = Convert.ToInt64(args[1]);
int taskCount = Convert.ToInt32(args[2]);
Func<object, List<long>> FindPrintFunc = (objRange) =>
{
long[] range = (long[])objRange;
List<long> found = new List<long>();
for(long i = range[0]; i < range[1]; i++)
{
if(IsPrime(i))
{
found.Add(i);
}
}
return found;
};
Task<List<long>>[] tasks = new Task<List<long>>[taskCount];
long currentFrom = from;
long currentTo = to / tasks.Length;
for(int i = 0; i < tasks.Length; i++)w
{
Console.WriteLine("Tasks[{0}] : {1} ~ {2}", i ,currentFrom, currentTo);
tasks[i] = new Task<List<long>>(FindPrintFunc, new long[]{currentFrom, currentTo});
currentFrom = currentTo + 1;
if(i == tasks.Length - 2)
{
currentTo = to;
}
else
{
currentTo = currentTo + (to / tasks.Length);
}
}
Console.WriteLine("Please press enter to start");
Console.ReadLine();
Console.WriteLine("Started");
DateTime startTime = DateTime.Now;
foreach(Task<List<long>> task in tasks)
{
task.Start();
}
List<long> total = new List<long>();
foreach (Task<List<long>> task in tasks)
{
task.Wait();
total.AddRange(task.Result.ToArray());
}
DateTime endTime = DateTime.Now;
TimeSpan ellapsed = endTime- startTime;
Console.WriteLine("Prime number between {0} and {1}", from, to, total.Count);
Console.WriteLine("Ellapsed time: {0}", ellapsed);
}
static bool IsPrime(long number)
{
if(number < 2)
{
return false;
}
if(number % 2 == 0 && number != 2)
{
return false;
}
for (long i = 2; i < number; i++)
{
if(number % i == 0)
{
return false;
}
}
return true;
}
}
}
728x90
'C# > 책 예제' 카테고리의 다른 글
C# Task async 한정자와 await 연산자 사용하기. (0) | 2019.06.08 |
---|---|
C# Task Parallel 클래스 사용하기. (0) | 2019.06.08 |
C# Task Task 클래스 사용하기. (0) | 2019.06.08 |
C# Thread Monitor.Wait 그리고 Monitor.Pulse 사용하기 (0) | 2019.06.08 |
C# Thread Monitor 키워드를 이용한 동기화 (0) | 2019.06.08 |
댓글