国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Part86to88TalkingaboutMultithreadinginC#

2019-11-14 13:59:59
字體:
來源:轉載
供稿:網友

Part 86   Multithreading in C#

What is a PRocess:

Process is what the Operating system uses to facilitate(幫助) the execution of a program by providing the resources required.Each process has unique process Id associated with(關聯) it. You can view the process within which a program is being executed using windows task manager.

What is Thread:

Thread is a light weight(輕量級) process.A process has at least one thread which is commonly called(通常被稱為) as main thread which actually executes the application code. A single process can hava multiple threads.

Please Note: All the threading related classes are present in(存在于) System.Threading namespace.

Part 87   Advantages and disadvantages of multithreading

Advantages of multithreading:

1, To maintain a responsive user interface(快速響應用戶界面)

2, To make effcient use of processor time while waiting for I/O operations to complete.(適當的利用處理器,在等待I / O操作完成的這段時間。)

3, To split large, CPU-bound tasks to be processed simultaneously on a machine that has multiple processors/cores(分割大cpu密集型任務處理的機器上同時有多個處理器/核心)

Disadvantages of multithreading:

1, On a single processor/core machine threading can affect performance negatively as there is overhead involved with context-switching.(在單個處理器/核心的機器,線程會對上下文切換開銷的性能有負面影響)

2, Have to write more lines of code to accomplish the same task.(需要編寫更多的代碼來完成相同的任務)

3,Multithreaded applications are difficult to write, understand, debug and maintain.(多線程應用程序很難寫,理解、調試和維護。)

Please Note: Only use multithreading when the advantages of doing so outweigh the disavantages.

Part 88   ThreadStart delegate

To create a Thread, create an instance of Thread class and to it's constructor pass the name of the function that we want the thread to execute.

class Program    {        static void Main(string[] args)        {            Thread t = new Thread(Number.Print);            t.Start();        }    }    public class Number    {        public static void Print()        {            for(int i=0;i<10;i++)            {                Console.WriteLine(i);            }        }    }
Thread

 Thread t = new Thread(Number.Print);
 t.Start();

We can rewrite the above line using ThreadStart delegate as shown below

Thead t1 = new Thread(new ThreadStart(Number.Print));

t1.Start();

Why a delegate need to be passed as a parameter to the Thread class constructor?

The purpose of creating a Thread is to execute a function. A delegate is a type safe function pointer, meaning it points to a function that the thread has to execute. In short, all threads require an entry point to start execution. Any thread you create will need an explicitly defined entry point i.e(那就是說) a pointer to the function where they should begin execution. So threads always require a delegate.

In the code below, we are not explicitly creating the ThreadStart delegage, then how is it working here?

Thread t1 = new Thread(Number.Print);

t1.Start();

It's working in spite of(盡管) not creating the ThreadStart delegage explictly because the framework is doing it automatically for us.

Other ways, We can also rewrite the same line using delegate() keyWord

Thread t = new Thread(delegate(){Number.Print();});

The same line rewritten using lambda expression

Thread t = new Thread(()=>Number.Print());

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临城县| 罗田县| 台东县| 民乐县| 那曲县| 遂川县| 监利县| 澄江县| 枣庄市| 邯郸县| 平泉县| 神木县| 邵东县| 兴隆县| 乃东县| 锡林浩特市| 莱阳市| 镇宁| 柘城县| 黄大仙区| 高密市| 平武县| 周至县| 教育| 桃园县| 若羌县| 博客| 游戏| 赞皇县| 伊春市| 余姚市| 广昌县| 额敏县| 盐城市| 西昌市| 塘沽区| 湖州市| 仙游县| 徐闻县| 桂平市| 贵德县|