天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

關于C#線程,線程池和并行運算的簡單使用和對比

前言:看了書上兩個使用C#4.0并行編程的demo,又對照以前收藏的網上幾篇講述線程池的雄文,一并整理,寫個示例總結一下。寫這篇文章的時候,發現關于線程的好幾個基礎的重要的知識點自己都不熟悉,而且可能習慣性認知淺薄,所以痛苦的無以復加,不知道到底要說什么。不想看文章的可以直接下載最后的示例,本文代碼主要參考Marc Clifton的“.NET's ThreadPool Class - Behind The Scenes”,對新手也許有幫助。

  參考:

  http://msdn.microsoft.com/zh-cn/library/system.threading.threadpool(VS.80).ASPx

  http://www.codeproject.com/KB/threads/threadtests.ASPx

  http://www.codeproject.com/KB/threads/smartthreadpool.ASPx

  http://blog.zhaojie.me/2009/07/thread-pool-1-the-goal-and-the-clr-thread-pool.html  (老趙的淺談線程池上中下三篇)

  先大概看一下控制臺應用程序的Main方法的主要代碼:

        static bool done = false;
static decimal count2 = 0;
static int threadDone = 0;//標志啟用線程數?
static System.Timers.Timer timer = new System.Timers.Timer(1000);

static decimal[] threadPoolCounters = new decimal[10];
static Thread[] threads = new Thread[10];
static System.Timers.Timer[] threadTimers = new System.Timers.Timer[10];

static void Main(string[] args)
{
timer.Stop();
/*當 AutoReset 設置為 false 時,Timer 只在第一個 Interval 過后引發一次 Elapsed 事件。
若要保持以 Interval 時間間隔引發 Elapsed 事件,請將 AutoReset 設置為 true。
*/
timer.AutoReset
= false;
timer.Elapsed
+= new ElapsedEventHandler(OnTimerEvent);//當timer.Start()時,觸發事件
decimal total = 0;

// raw test
decimal count1 = SingleThreadTest();//單一線程,一跑到底
Console.WriteLine("Single thread count = " + count1.ToString());

// create one thread, increment counter, destroy thread, repeat
Console.WriteLine();
CreateAndDestroyTest();
//創建一個線程,運算,然后銷毀該線程 重復前面的動作
Console.WriteLine("Create and destroy per count = " + count2.ToString());

// Create 10 threads and run them simultaneously
//一次性創建10個線程,然后遍歷使線程執行運算
Console.WriteLine();
InitThreadPoolCounters();
InitThreads();
StartThreads();
while (threadDone != 10) { };
Console.WriteLine(
"10 simultaneous threads:");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(
"T" + i.ToString() + " = " + threadPoolCounters[i].ToString() + " ");
total
+= threadPoolCounters[i];
}
Console.WriteLine(
"Total = " + total.ToString());
Console.WriteLine();

Console.WriteLine(
"///////////////////////////////////////////////////");

// using ThreadPool
//直接通過線程池的QueueUserWorkItem方法,按隊列執行10個任務
Console.WriteLine();
Console.WriteLine(
"ThreadPool:");
InitThreadPoolCounters();
QueueThreadPoolThreads();
while (threadDone != 10) { };
Console.WriteLine(
"ThreadPool: 10 simultaneous threads:");
total
= 0;
for (int i = 0; i < 10; i++)
{
// threadTimers[i].Stop();
// threadTimers[i].Dispose();
Console.WriteLine("T" + i.ToString() + " = " + threadPoolCounters[i].ToString() + " ");
total
+= threadPoolCounters[i];
}
Console.WriteLine(
"Total = " + total.ToString());

// using SmartThreadPool
//通過Amir Bar的SmartThreadPool線程池,利用QueueUserWorkItem方法,按隊列執行10個任務
Console.WriteLine();
Console.WriteLine(
"SmartThreadPool:");
InitThreadPoolCounters();
QueueSmartThreadPoolThreads();
while (threadDone != 10) { };
Console.WriteLine(
"SmartThreadPool: 10 simultaneous threads:");
total
= 0;
for (int i = 0; i < 10; i++)
{
Console.WriteLine(
"T" + i.ToString() + " = " + threadPoolCounters[i].ToString() + " ");
total
+= threadPoolCounters[i];
}
Console.WriteLine(
"Total = " + total.ToString());

// using ManagedThreadPool
//通過Stephen Toub改進后的線程池,利用QueueUserWorkItem方法,按隊列執行10個任務
Console.WriteLine();
Console.WriteLine(
"ManagedThreadPool:");
InitThreadPoolCounters();
QueueManagedThreadPoolThreads();
while (threadDone != 10) { };
Console.WriteLine(
"ManagedThreadPool: 10 simultaneous threads:");
total
= 0;
for (int i = 0; i < 10; i++)
{
Console.WriteLine(
"T" + i.ToString() + " = " + threadPoolCounters[i].ToString() + " ");
total
+= threadPoolCounters[i];
}
Console.WriteLine(
"Total = " + total.ToString());

// using C#4.0 Parallel
//通過Tasks.Parallel.For進行并行運算
Console.WriteLine();
Console.WriteLine(
"Parallel:");
InitThreadPoolCounters();
UseParallelTasks();
while (threadDone != 10) { };
Console.WriteLine(
"Parallel: 10 simultaneous threads:");
total
= 0;
for (int i = 0; i < 10; i++)
{
Console.WriteLine(
"T" + i.ToString() + " = " + threadPoolCounters[i].ToString() + " ");
total
+= threadPoolCounters[i];
}
Console.WriteLine(
"Total = " + total.ToString());
}

NET技術關于C#線程,線程池和并行運算的簡單使用和對比,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 色人阁久久 | 国产人妻麻豆蜜桃色在线 | 一日本道伊人久久综合影 | 免费国产综合视频在线看 | 亚洲精品美女久久777777 | 男人J进入女人P免费狂躁 | 十分钟免费看完整视频 | 欧美在线视频一区 | 日韩娇小性hd | 果冻传媒视频在线观看完整版免费 | 第七色男人天堂 | 吸奶舔下面 | 色哦色哦哦色天天综合 | 国产成人精品综合在线 | 国产亚洲精品视频在线网 | 精品视频免费在线观看 | 国产99青草全福视在线 | 日本熟妇乱妇熟色A片蜜桃亚洲 | 午夜伦理:伦理片 | 久久成人免费观看草草影院 | 日本无卡无吗在线 | 娇妻归来在线观看免费完整版电影 | 琪琪see色原网色原网站 | 日韩欧美中文字幕在线二视频 | 国产成人免费a在线视频app | 无遮掩H黄纯肉动漫在线观看星 | 国产成人精品精品欧美 | 最新影音先锋av资源台 | 久久高清一本无码 | 亚洲三级视频在线观看 | 老女老肥熟国产在线视频 | 欲乱艳荡少寡妇全文免费 | 久久99精品涩AV毛片观看 | 亚洲AV中文字幕无码久久 | 国产精自产拍久久久久久蜜 | 久热久热精品在线观看 | 琪琪色原网20岁以下热热色原网站 | 伊人亚洲综合青草青草久热 | qvod电影| 亚洲嫩草AV永久无码精品无码 | 免费a视频在线观看 |