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 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());
新聞熱點
疑難解答