一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

前言:看了書上兩個使用C#4.0并行編程的demo,又對照以前收藏的網(wǎng)上幾篇講述線程池的雄文,一并整理,寫個示例總結(jié)一下。寫這篇文章的時候,發(fā)現(xiàn)關(guān)于線程的好幾個基礎(chǔ)的重要的知識點自己都不熟悉,而且可能習慣性認知淺薄,所以痛苦的無以復(fù)加,不知道到底要說什么。不想看文章的可以直接下載最后的示例,本文代碼主要參考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  (老趙的淺談線程池上中下三篇)

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

        static bool done = false;
static decimal count2 = 0;
static int threadDone = 0;//標志啟用線程數(shù)?
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 設(shè)置為 false 時,Timer 只在第一個 Interval 過后引發(fā)一次 Elapsed 事件。
若要保持以 Interval 時間間隔引發(fā) Elapsed 事件,請將 AutoReset 設(shè)置為 true。
*/
timer.AutoReset
= false;
timer.Elapsed
+= new ElapsedEventHandler(OnTimerEvent);//當timer.Start()時,觸發(fā)事件
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();
//創(chuàng)建一個線程,運算,然后銷毀該線程 重復(fù)前面的動作
Console.WriteLine("Create and destroy per count = " + count2.ToString());

// Create 10 threads and run them simultaneously
//一次性創(chuàng)建10個線程,然后遍歷使線程執(zhí)行運算
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方法,按隊列執(zhí)行10個任務(wù)
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方法,按隊列執(zhí)行10個任務(wù)
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方法,按隊列執(zhí)行10個任務(wù)
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技術(shù)關(guān)于C#線程,線程池和并行運算的簡單使用和對比,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 欧美一级xxxx俄罗斯一级 | 一区二区三区四区视频 | 久久er国产精品免费观看1 | 精品综合久久久久久8888 | 99精品国产闺蜜国产在线闺蜜 | 色视频一区二区三区 | 欧美在线观看黄色 | 四虎永久在线精品国产免费 | 91免费版视频 | 91精品国产免费青青碰在线观看 | vr欧美乱强伦xxxxx | 91精品全国免费观看含羞草 | 在线二区 | 国产精品视频一区二区亚瑟 | 热久久国产欧美一区二区精品 | 亚洲综合图片人成综合网 | adc影院在线观看成人 | 午夜久久久久久网站 | 国产真实乱在线更新 | 免费成人小视频 | 成人影院一区二区三区 | 一级一级特黄女人精品毛片视频 | 狠狠穞老司机的福67194 | 国产亚洲一区二区在线观看 | 色哟哟在线免费观看 | 成年ssswww中国女人 | 欧美成人伊人十综合色 | 黄色小视频网 | 午夜看毛片 | 成 人 黄 色视频免费播放 | 精品国产高清自在线一区二区三区 | 亚洲精品午夜级久久久久 | 欧美精品亚洲精品日韩经典 | 色综合色狠狠天天久久婷婷基地 | 国产成人精品一区二区仙踪林 | 国产永久免费视频 | 伊人久久综合谁合综合久久 | 中文字幕123区 | 久久国产乱子伦精品免费午夜 | 日本网络视频www色高清免费 | 91视频聊天网 |