Leanpub Header

Skip to main content

由 Parallel.For 看多執行緒程式設計

.NET / C# 平行程式設計系列

C# 平行處理

Minimum price

$44.00

$44.00

You pay

$44.00

Author earns

$35.20
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
About

About

About the Book

關於本書

某日在網路上爬文, 看到了臉書社團 這個[網址](https://bit.ly/3mNMSNJ) 中有許多人正在熱烈的討論關於如何使用 Parallel.For 這個方法來做到平行執行一萬次 Thread.Sleep(5000) 需求。 這是一個很有趣的題目,然而卻看到大家在這個串列中盡情的抒發自己的意見,表達各種看法,但是卻沒有人可以正確地說明如何做到提問者想要解決的問題, 計算機科學領域沒有這麼模寧兩可與複雜,只要有者正確的觀念與技巧, 都可以解釋當時發生了什麼問題,以及使用正確的方法來解決這些問題,然而,卻沒有透過這些討論內容來得到任何技能與知識提升的效果。

很早以前就想要寫本關於 .NET / C# 平行程式設計 方面的書籍,不過這方面的內容相當的多與繁雜,但是,已經針對這些內容整理與設計出四個階段的教育訓練課程與超過上百個講解與練習範例專案專案,因此,就逐一根據這個討論串列中各個人所提出的講法,設計不同的範例程式碼,而我只是想單純的想做個簡單的學術研究,並且將這些範例程式碼設計彙整成為一份教學課程簡報。

原本只是想針對 Parallel.For 這個題目設計一個簡單的快閃課程,有機會與大家一起來分享這些知識,但是在設計教學簡報的過程中,發現到有許多基礎的知識要能夠交代,才能夠順利解決某些問題,導致於簡報內容設計的越來越多,因此想說索性把這些過程與內容寫成一本書吧。

在此因緣際會下,開始了 .NET / C# 平行程式設計系列 的第一本書 由 Parallel.For 看多執行緒程式設計,在這本書裡面,將會提供 6 章節,將會討論到底下主題

  • 我只是想做個簡單的學術研究針對該討論串列內容與主要關鍵核心技術先行說明一番
  • 了解 Parallel.For 執行順序首先,先來了解 Parallel.For 是什麼,是要設計解決什麼問題
  • Parallel.For 平行迴圈程式設計的指定平行工作數目上限特性理解平行度,也就是在進行平行資料處理,原則上會把這些資料集和切割成不同區塊,交由不同的執行緒來執行緒
  • 使用Parallel.For做到一萬次迴圈,實際需要花多少時間將問題提問者提出的測試程式碼跑一次,就會知道提問者想要解決什麼問題了
  • 重現造成記憶體不足情況當提到記憶體不足,究竟說的是哪一個?Memory Overflow / Stack Overflow ,而造成的原因是什麼?
  • Parallel.For 同時執行 10000 次迴圈,觀察用了多少執行緒雖說 Parallel.For 可以提供資料平行處理能力,但是,究竟產生了多少執行緒來做到平行執行呢?
  • Parallel.For同時執行 200 次要花多久時間與用到多少執行緒Parallel.For 平行迴圈一萬次僅用到 235 執行緒,那麼,若僅平行迴圈 200 次,會不會5秒就執行完畢
  • 透過執行緒集區(預設參數)取得過多執行緒的使用情況了解執行緒集區的預設執行方式
  • 透過執行緒集區(修正參數)取得過多執行緒的使用情況設定執行緒集區預設準備 500 個執行緒,看看執行結果
  • 使用 new Thread 來自行產生 10000 個執行緒來使用大家都說執行使用執行緒,效能會很好,那麼就來產生 10000 個執行緒來看看表現
  • 了解同時使用 10000 個執行緒,這些執行緒開始執行時候,會造成多少延遲時間因為直接產生 10000 個執行緒效能表現不佳,了解問題在哪裡
  • Parallel.For 透過執行緒集區準備10000個執行緒的結果設定執行緒集區預設準備 10000 個執行緒,執行 Parallel.For 一萬次,看看執行結果
  • Parallel.For 透過執行緒集區準備不同執行緒數量的32位元執行結果在 32 位元下,若產生過多執行緒,會有記憶體不足問題產生
  • 聽說 Task 很厲害,那就使用 10000 個Tasks絕大部分的人都對 Task 與 Thread 有所誤解,在此建 10000 個 Task 來看看表現
  • 用 Task.Factory.StartNew 建立 10000 個Tasks使用每個 Task 都是自己生成的執行緒,不透過預設工作排程器來取得執行緒,看看表現
  • 使用 Task.Delay 來取代 Thread.Sleep 的封鎖 Block 等待使用非封鎖的等待方式來體驗好的設計方法有什麼不同
  • Parallel.For 改成使用 Task.Delay最後,了解如何做到資料平行 10000 次處理,最終真的僅需要 5 秒就可以執行完畢的方法

誰適合閱讀這本書

  • 對於任何想要學習 .NET / C# 平行程式設計 這個技術的開發者
  • 需要具有 .NET / C# 開發經驗者,畢竟這本書中的例子是使用 C# 解說
  • 最好能夠具備基本的計算機概念與知識

Share this book

Categories

Author

About the Author

Vulcan Lee

從 2012 年開始進行 .NET / C# 教育訓練課程之後,對於許多開發出來的課程,並不是隨便敷衍的設計出來,這其中我會不斷地針對已經開發出來的課程來進行修正、改版、追加內容,因此,相關的課程是不斷的在變化與強化的。

要完成這樣的一系列課程是一種自我挑戰,讓自己可以看得更高、看得更遠、讓抄襲者永遠無法模仿,因為,這些課程加入了許多巧思與技能在裡面,內行看門道、外行看熱鬧。

我所設計的教學課程,為了要能夠讓入門者、想要精通者都可以喜歡我設計的課程,將會把課程應該具有的特色,所提供的內容將會涵蓋到更多的層面、更多範例碼、更多觀念介紹、更多的動畫來理解艱澀技術應用、更多挑戰應用、更多的日常遇到問題與解決方法、更完整的開發設計指引。

因此,很期望到時候能夠與大家一起來進行這個課程的交流與互動,也希望大家可以從這些課程學到更多知識與經驗,應用在日常開發專案上。

若對於這些課程有興趣,或者有任何其他需求,可以到 Xamarin Blazor 實驗室 粉絲團來私訊給我

也歡迎來查看我寫的部落格文章 https://csharpkh.blogspot.com/

Contents

Table of Contents

關於本書

  1. 誰適合閱讀這本書
  2. 練習專案原始碼

1.我只是想做個簡單的學術研究

  1. 1.1了解 Parallel.For 執行順序
  2. 1.2Parallel.For 平行迴圈程式設計的指定平行工作數目上限特性

2.有人提出問一個 Parallel.For 問題

  1. 2.1執行結果分析
  2. 2.2Parallel.For 是如何使用執行緒
  3. 2.3重現造成記憶體不足情況
  4. 2.4Memory Overflow 造成的原因
  5. 2.5Stack Overflow 造成的原因

3.Parallel.For 同時執行 10000 次,觀察用了多少執行緒

  1. 3.1Parallel.For同時執行 200 次要花多久時間與用到多少執行緒
  2. 3.2透過執行緒集區(預設參數)取得過多執行緒的使用情況
  3. 3.3透過執行緒集區(修正參數)取得過多執行緒的使用情況

4.使用 new Thread 來自行產生 10000 個執行緒來使用

  1. 4.1了解同時使用 10000 個執行緒,這些執行緒開始執行時候,會造成多少延遲時間
  2. 4.2Parallel.For 透過執行緒集區準備10000個執行緒的結果
  3. 4.3Parallel.For 透過執行緒集區準備不同執行緒數量的32位元執行結果

5.聽說 Task 很厲害,那就使用 10000 個Tasks

  1. 5.1用Task.Factory.StartNew 建立 10000 個Tasks

6.使用 Task.Delay 來取代 Thread.Sleep 的封鎖 Block 等待

  1. 6.1Parallel.For 改成使用 Task.Delay
  2. 6.2修正 Parallel.For 使用 Task.Delay 造成的錯誤
  3. 6.3確認使用到多少的工作與執行緒

版權頁

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub