一、主線程算法
進程建立時,默認建立一個線程,這個線程就是主線程。主線程是產生其餘子線程的線程,同時,主線程必須是最後一個結束執行的線程,它完成各類關閉其餘子線程的操做。儘管主線程是程序開始時自動建立的,它也能夠經過Thead類對象來控制,經過調用 CurrentThread方法得到當前線程的引用安全
二、多線程的優缺點多線程
優勢:異步
----提升應用程序響應。
----使多CPU系統更加有效。操做系統會保證當線程數不大於CPU數目時,不一樣的線程運行於不一樣的CPU上。
----改善程序結構。一個既長又複雜的進程能夠考慮分爲多個線程,成爲幾個獨立或半獨立的運行部分,這樣的程序會利於理解和修改。函數
缺點:ui
很差控制、系統運行具備不肯定性this
三、多線程適用的場合spa
四、C#之Thread操作系統
Thread: 建立並控制線程,設置其優先級並獲取其狀態。線程
經常使用方法:
Start() :讓操做系統將當前實例的狀態更改成 ThreadState.Running。該線程的狀態必須爲 Running時,操做系統才能夠調度該線程。
一旦線程處於 ThreadState.Running 狀態,操做系統就能夠安排其執行。 線程從方法的第一行(由提供給線程構造函數的 ThreadStart 或 ParameterizedThreadStart 委託表示)開始執行。線程一旦終止,它就沒法經過再次調用 Start 來從新啓動。
爲了能在啓動線程時動態的爲線程傳遞參數,可使用ParameterizedThreadStart 委託,傳遞給 Start(Object) 方法的對象將傳遞給該委託。 ParameterizedThreadStart 委託和 Thread.Start(Object) 方法重載使得將數據傳遞給線程過程變得簡單,但因爲能夠將任何對象傳遞給 Thread.Start(Object),所以這種方法並非類型安全的。 將數據傳遞給線程過程的一個更可靠的方法是將線程過程和數據字段都放入輔助對象。一種替代方法是將線程過程和數據封裝在幫助器類中,並使用 ThreadStart 委託執行線程過程。這兩個委託都沒有返回值,由於沒有地方用於從異步調用中返回數據。 爲檢索線程方法的結果,可使用回調方法。
參數傳遞:
將須要傳遞的參數放在對象中public class ThreadWithState
{
// State information used in the task.
private string boilerplate;
private int value;
// The constructor obtains the state information.
public ThreadWithState(string text, int number)
{
boilerplate = text;
value = number;
}
// The thread procedure performs the task, such as formatting
// and printing a document.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
}
}
// Entry point for the example.
//
public class Example
{
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.", 42);
// Create a thread to execute the task, and then
// start the thread.
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
}
}
回調獲取線程執行結果
回調獲取線程執行結果
使用回調獲取線程的執行結果public class ThreadWithState
{
// State information used in the task.
private string boilerplate;
private int value;
// Delegate used to execute the callback method when the
// task is complete.
private ExampleCallback callback;
// The constructor obtains the state information and the
// callback delegate.
public ThreadWithState(string text, int number,
ExampleCallback callbackDelegate)
{
boilerplate = text;
value = number;
callback = callbackDelegate;
}
// The thread procedure performs the task, such as
// formatting and printing a document, and then invokes
// the callback delegate with the number of lines printed.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
if (callback != null)
callback(1);
}
}
// Delegate that defines the signature for the callback method.
//
public delegate void ExampleCallback(int lineCount);
// Entry point for the example.
//
public class Example
{
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.",
42,
new ExampleCallback(ResultCallback)
);
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
}
// The callback method must match the signature of the
// callback delegate.
//
public static void ResultCallback(int lineCount)
{
Console.WriteLine(
"Independent task printed {0} lines.", lineCount);
}
}
Thread.Sleep()
調用 Thread.Sleep 方法會致使當前線程當即阻止,阻止時間的長度等於傳遞給 Thread.Sleep 的毫秒數,這樣,就會將其時間片中剩餘的部分讓與另外一個線程。 一個線程不能針對另外一個線程調用 Thread.Sleep。
Interrupt():中斷處於 WaitSleepJoin 線程狀態的線程。
Suspend和Resume(已過期):掛起和繼續
在 .NET Framework 2.0 版中,Thread.Suspend 和 Thread.Resume 方法已標記爲過期,並將從將來版本中移除。
Abort():方法用於永久地中止託管線程。一旦線程被停止,它將沒法從新啓動。
Join():阻塞調用線程,直到某個線程終止時爲止。
ThreadPriority(優先級)
指定 Thread 的調度優先級。
ThreadPriority 定義一組線程優先級的全部可能值。線程優先級指定一個線程相對於另外一個線程的相對優先級。
每一個線程都有一個分配的優先級。在運行庫內建立的線程最初被分配 Normal 優先級,而在運行庫外建立的線程在進入運行庫時將保留其先前的優先級。能夠經過訪問線程的 Priority 屬性來獲取和設置其優先級。
根據線程的優先級調度線程的執行。用於肯定線程執行順序的調度算法隨操做系統的不一樣而不一樣。操做系統也能夠在用戶界面的焦點在前臺和後臺之間移動時動態地調整線程的優先級。
一個線程的優先級不影響該線程的狀態;
五、線程的前臺與後臺
.Net的公用語言運行時(Common Language Runtime,CLR)能區分兩種不一樣類型的線程:前臺線程和後臺線程。這二者的區別就是:應用程序必須運行完全部的前臺線程才能夠退出;而對於後臺線程,應用程序則能夠不考慮其是否已經運行完畢而直接退出,全部的後臺線程在應用程序退出時都會自動結束。
一個線程是前臺線程仍是後臺線程可由它的IsBackground屬性來決定。這個屬性是可讀又可寫的。它的默認值爲false,即意味着一個線程默認爲前臺線程。咱們能夠將它的IsBackground屬性設置爲true,從而使之成爲一個後臺線程。