0

我在一个数组中有多个任务,用于计算给定范围内的素数。为了比较任务与线程性能,我想在任务中使用线程,然后检查性能统计信息。

线程将如何用于任务,到目前为止,这就是我所做的:

public Form1()
    {
        InitializeComponent();

        cpuCounter = new PerformanceCounter();

        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

        ramCounter = new PerformanceCounter("Memory", "Available MBytes");

        this.scheduler = TaskScheduler.FromCurrentSynchronizationContext();

        this.numericUpDown1.Maximum = int.MaxValue;
    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        //get the lower and upper bounds for prime range
        int lower = int.Parse(this.numericUpDown1.Value.ToString());
        int upper = 0 ;

        //get the time in milliseconds for task deadline
        int taskDeadline = int.Parse(this.time.Text);

        //determine tasks completed
        int tasksCompleted = 0;

        Random random = new Random();

        for (int taskCount = 1; taskCount <= 1; ++taskCount)
        {
            int taskArraySize = taskCount * 100;
            Task[] taskArray = new Task[taskArraySize];

            this.txtNumOfPrimes.Text += "Performing test for " +  
                 taskArraySize.ToString() + 
                 " tasks" + 
                 Environment.NewLine + 
                 Environment.NewLine; 

            for (int i = 0; i < taskArray.Length; i++)
            {
                upper = random.Next(5, 10);
                taskArray[i] = new Task(() => getPrimesInRange(lower, upper));
                taskArray[i].Start();

                bool timeout = taskArray[i].Wait(taskDeadline);

                if (!timeout)
                {
                    // If it hasn't finished at timeout display message
                    this.txtNumOfPrimes.Text += 
                        "Message to User: Task not completed, Status=> " + 
                        taskArray[i].Status.ToString() + 
                        Environment.NewLine;

                }

                else
                {
                    this.txtNumOfPrimes.Text += "Task completed in timeout " + 
                         ", CPU usage: " + this.getCurrentCpuUsage() + 
                         ", RAM usage: " + 
                         this.getAvailableRAM() + 
                         Environment.NewLine;

                    tasksCompleted++;
                }
            }
        }



        this.txtNumOfPrimes.Text += Environment.NewLine;
        this.txtNumOfPrimes.Text += 
            "Tasks Completed: " + 
            tasksCompleted.ToString() + 
            Environment.NewLine;
    }
4

1 回答 1

1

任务的全部意义在于“简化向应用程序添加并行性和并发性的过程”。确实(来自http://msdn.microsoft.com/en-us/library/dd537609):

在幕后,任务排队到线程池,该线程池已通过算法(如爬山)进行了增强,该算法可确定并调整线程数以最大限度地提高吞吐量。这使得任务相对轻量级,您可以创建许多任务以启用细粒度并行性。为了补充这一点,采用了广为人知的工作窃取算法来提供负载平衡。

简而言之,任务完成线程工作而没有太多麻烦和繁琐的工作。

要比较两者,请考虑将Parrallel.ForEach用于任务。例如:

public class PrimeRange
{
    public int Start;
    public int Snd;
}

List<PrimeRange> primes = new []
{
    new PrimeRange{Start = 0, End = 1000},
    new PrimeRange{Start = 1001, End = 2000}
    // An so on
};
Parallel.ForEach(primes, x => CalculatePrimes(x, OnResult())));

whereCalculatePrimes是一个方法,PrimeRange在计算素数时需要一个和一个委托来调用。Parraler.ForEach 将为每个素数元素启动一个任务并CalculatePrimes()在其上运行并为您处理线程分配和调度。

要将其与线程进行比较,请使用以下内容:

List<Thread> threads = new List<Thread>();
foreach(PrimeRange primeRange in primes)
{
    threads = new Thread(CalculatePrimes).Start(x);
}
foreach(var thread in threads)
{
    thread.Join();
}    

其中CalculatePrimes 还需要存储结果(或类似的东西)。有关等待正在运行的线程的更多信息,请参阅C# 等待多个线程完成

您可以使用StopWatch对结果进行计时。

于 2012-09-12T11:42:34.377 回答