国产探花免费观看_亚洲丰满少妇自慰呻吟_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());

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平舆县| 望城县| 泗水县| 米林县| 资中县| 海林市| 绍兴市| 清水河县| 潮州市| 万全县| 磐安县| 桃源县| 卫辉市| 永昌县| 云龙县| 延安市| 枣强县| 彭泽县| 广南县| 肥城市| 商洛市| 邵武市| 旅游| 永嘉县| 正宁县| 洛阳市| 星子县| 喜德县| 米泉市| 宁强县| 陆良县| 湘乡市| 龙岩市| 手机| 阿鲁科尔沁旗| 大城县| 年辖:市辖区| 临泽县| 蛟河市| 上蔡县| 咸丰县|