C#多線程的幾種方法詳解示例


1 進程、線程、同步、異步的概念
2 回顧委托,開始異步
3 異步多線程的三大特點

異步多線程都覺得很厲害,也是面試必備,高級開發必備技能
多線程很熟悉/經常在用的 ?
多線程在用,但是很懵 ?
一直沒怎么敢用的 ?

多線程在.Net不同的版本里面,都在不斷的升級
單進程多線程的模型

進程:計算機概念,把程序運行時占用的全部計算資源,叫做進程
線程:計算機概念,程序執行的最小單位,包含自己使用的各種計算資源
多線程:計算機概念,多個線程同時執行(上傳畫面/聲音上傳/獲取聊天消息)
一個進程可以包含多個線程,線程是隸屬於進程的

C#是面向對象語言,萬物皆對象,類來映射現實生活中的物體
Thread就是線程類
多個Thread並發執行,就是多線程

 

委托的異步調用
1 同步方法卡界面,Winform的UI線程在忙於計算,不能響應別的操作
異步多線程方法不卡界面,UI線程閑置,計算任務交給其他線程執行
Winform--點擊個按鈕不希望界面卡死
Web--寫文本日志,發郵件,這正耗時操作其實可以多線程的

2 同步方法慢(14155),只有一個線程計算
異步多線程方法快(5144),多個線程並發計算
一般認為計算機是精准的,5個線程比1個線程 那應該是5倍?
多線程是用資源換性能,
a資源有限(一般來說資源夠的) b線程調度管理損耗 性能不是線性增長,
速度快的應用太多了,
有個大表查詢很慢,能不能多線程優化下,提升下性能? T/F 不行,任務不能拆分
一個數據庫查詢,一個接口調用,一個硬盤讀寫,一個相加計算,可以,任務是獨立運行的
但是線程不是越多越好

3 多線程的無序性:
啟動無序:線程是計算機資源,線程申請是操作系統調度管理的,隨機了!
同一個線程執行同一個任務時間不確定:CPU分片,看運氣(線程優先級)
結束無序:以上疊加
這個是提請注意,很多時候多線程操作,但是又有順序要求,
延遲一下啟動? 不靠譜, 線程優先級也不靠譜
也許運行1w次都是對的,但是隨着業務的變化 數據量的增加 服務器的升級,執行時間都是會變的
以前運行的好好的,偶爾就錯一下,或者數據量大了,總是錯

線程順序的控制,且聽下回分解

(0)線程池

復制代碼
 //線程池
    class Thread2
    {
        public void ThreadMain()
        {
            ThreadDemoClass demoClass = new ThreadDemoClass();

            //設置當沒有請求時線程池維護的空閑線程數
            //第一個參數為輔助線程數
            //第二個參數為異步 I/O 線程數
            ThreadPool.SetMinThreads(5, 5);

            //設置同時處於活動狀態的線程池的線程數,所有大於次數目的請求將保持排隊狀態,直到線程池變為可用
            //第一個參數為輔助線程數
            //第二個參數為異步 I/O 線程數
            ThreadPool.SetMaxThreads(100, 100);

            //使用委托綁定線程池要執行的方法(無參數)
            WaitCallback waitCallback1 = new WaitCallback(demoClass.Run1);
            //將方法排入隊列,在線程池變為可用時執行
            ThreadPool.QueueUserWorkItem(waitCallback1);


            //使用委托綁定線程池要執行的方法(有參數)
            WaitCallback waitCallback2 = new WaitCallback(demoClass.Run1);
            //將方法排入隊列,在線程池變為可用時執行
            ThreadPool.QueueUserWorkItem(waitCallback2, "張三");


            UserInfo userInfo = new UserInfo();
            userInfo.Name = "張三";
            userInfo.Age = 33;

            //使用委托綁定線程池要執行的方法(有參數,自定義類型的參數)
            WaitCallback waitCallback3 = new WaitCallback(demoClass.Run2);
            //將方法排入隊列,在線程池變為可用時執行
            ThreadPool.QueueUserWorkItem(waitCallback3, userInfo);

            Console.WriteLine();
            Console.WriteLine("主線程正在工作…");
            Console.WriteLine("主線程ID為:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Console.ReadKey();
        }

        //子線程類
        public class ThreadDemoClass
        {
            //子線程1
            public void Run1(object obj)
            {
                string name = obj as string;

                Console.WriteLine();
                Console.WriteLine("子線程正在工作…");
                Console.WriteLine("我的名字是 " + name);
                Console.WriteLine("子線程ID為:" + Thread.CurrentThread.ManagedThreadId.ToString());
            }

            //子線程2
            public void Run2(object obj)
            {
                UserInfo userInfo = (UserInfo)obj;

                Console.WriteLine();
                Console.WriteLine("子線程正在工作…");
                Console.WriteLine("我的名字是 " + userInfo.Name);
                Console.WriteLine("我今年" + userInfo.Age + "歲");
                Console.WriteLine("子線程ID為:" + Thread.CurrentThread.ManagedThreadId.ToString());
            }
        }

        public class UserInfo
        {
            public string Name { get; set; }

            public int Age { get; set; }
        }
    }
復制代碼

 

(1)不需要傳遞參數,也不需要返回參數

ThreadStart是一個委托,這個委托的定義為void ThreadStart(),沒有參數與返回值。

復制代碼

class Program

 

 
         

{

 
         

static void Main(string[] args)

 
         

{

 
         

for (int i = 0; i < 30; i++)

 
         

{

 
         

ThreadStart threadStart = new ThreadStart(Calculate);

 
         

Thread thread = new Thread(threadStart);

 
         

thread.Start();

 
         

}

 
         

Thread.Sleep(2000);

 
         

Console.Read();

 
         

}

 
         

public static void Calculate()

 
         

{

 
         

DateTime time = DateTime.Now;//得到當前時間

 
         

Random ra = new Random();//隨機數對象

 
         

Thread.Sleep(ra.Next(10,100));//隨機休眠一段時間

 
         

Console.WriteLine(time.Minute + ":" + time.Millisecond);

 
         

}

 
         

}

 
復制代碼

(2)需要傳遞單個參數

ParameterThreadStart委托定義為void ParameterizedThreadStart(object state),有一個參數但是沒有返回值。

復制代碼
class Program
{

static void Main(string[] args)

{

for (int i = 0; i < 30; i++)

{

ParameterizedThreadStart tStart = new ParameterizedThreadStart(Calculate);

Thread thread = new Thread(tStart);

thread.Start(i*10+10);//傳遞參數

}

Thread.Sleep(2000);

Console.Read();

}

public static void Calculate(object arg)

{

Random ra = new Random();//隨機數對象

Thread.Sleep(ra.Next(10, 100));//隨機休眠一段時間

Console.WriteLine(arg);

}

}
復制代碼

(3)使用專門的線程類(常用)

使用線程類可以有多個參數與多個返回值,十分靈活!

復制代碼
class Program
{

static void Main(string[] args)

{

MyThread mt = new MyThread(100);

ThreadStart threadStart = new ThreadStart(mt.Calculate);

Thread thread = new Thread(threadStart);

thread.Start();

   //等待線程結束

while (thread.ThreadState != ThreadState.Stopped)

{

Thread.Sleep(10);

}

Console.WriteLine(mt.Result);//打印返回值

Console.Read();

}

}

public class MyThread//線程類

{

public int Parame { set; get; }//參數

public int Result { set; get; }//返回值

//構造函數

public MyThread(int parame)

{

this.Parame = parame;

}

//線程執行方法

public void Calculate()

{

Random ra = new Random();//隨機數對象

Thread.Sleep(ra.Next(10, 100));//隨機休眠一段時間

Console.WriteLine(this.Parame);

this.Result = this.Parame * ra.Next(10, 100);

}

}
復制代碼

(4)使用匿名方法(常用)

使用匿名方法啟動線程可以有多個參數和返回值,而且使用非常方便!

復制代碼
class Program
{

static void Main(string[] args)

{

int Parame = 100;//當做參數

int Result = 0;//當做返回值

//匿名方法

ThreadStart threadStart = new ThreadStart(delegate()

{

Random ra = new Random();//隨機數對象

Thread.Sleep(ra.Next(10, 100));//隨機休眠一段時間

Console.WriteLine(Parame);//輸出參數

Result = Parame * ra.Next(10, 100);//計算返回值

});

Thread thread = new Thread(threadStart);

thread.Start();//多線程啟動匿名方法

//等待線程結束

while (thread.ThreadState != ThreadState.Stopped)

{

Thread.Sleep(10);

}

Console.WriteLine(Result);//打印返回值

Console.Read();

}
}
復制代碼

(5)使用委托開啟多線程(多線程深入)

1、用委托(Delegate)的BeginInvoke和EndInvoke方法操作線程

BeginInvoke方法可以使用線程異步地執行委托所指向的方法。然后通過EndInvoke方法獲得方法的返回值(EndInvoke方法的返回值就是被調用方法的返回值),或是確定方法已經被成功調用。

復制代碼
class Program
{

private delegate int NewTaskDelegate(int ms);

private static int newTask(int ms)

{

Console.WriteLine("任務開始");

Thread.Sleep(ms);

Random random = new Random();

int n = random.Next(10000);

Console.WriteLine("任務完成");

return n;

}

static void Main(string[] args)

{

NewTaskDelegate task = newTask;

IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

//EndInvoke方法將被阻塞2秒

int result = task.EndInvoke(asyncResult);

Console.WriteLine(result);

Console.Read();

}

}
復制代碼

2、使用IAsyncResult.IsCompleted屬性來判斷異步調用是否完成

復制代碼
class Program
{

private delegate int NewTaskDelegate(int ms);

private static int newTask(int ms)

{

Console.WriteLine("任務開始");

Thread.Sleep(ms);

Random random = new Random();

int n = random.Next(10000);

Console.WriteLine("任務完成");

return n;

}

static void Main(string[] args)

{

NewTaskDelegate task = newTask;

IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

//等待異步執行完成

while (!asyncResult.IsCompleted)

{

Console.Write("*");

Thread.Sleep(100);

}

// 由於異步調用已經完成,因此, EndInvoke會立刻返回結果

int result = task.EndInvoke(asyncResult);

Console.WriteLine(result);

Console.Read();

}

}
復制代碼

3、使用WaitOne方法等待異步方法執行完成

WaitOne的第一個參數表示要等待的毫秒數,在指定時間之內,WaitOne方法將一直等待,直到異步調用完成,並發出通知,WaitOne方法才返回true。當等待指定時間之后,異步調用仍未完成,WaitOne方法返回false,如果指定時間為0,表示不等待,如果為-1,表示永遠等待,直到異步調用完成。

復制代碼
class Program
{

private delegate int NewTaskDelegate(int ms);

private static int newTask(int ms)

{

Console.WriteLine("任務開始");

Thread.Sleep(ms);

Random random = new Random();

int n = random.Next(10000);

Console.WriteLine("任務完成");

return n;

}

static void Main(string[] args)

{

NewTaskDelegate task = newTask;

IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

//等待異步執行完成

while (!asyncResult.AsyncWaitHandle.WaitOne(100, false))

{

Console.Write("*");

}

int result = task.EndInvoke(asyncResult);

Console.WriteLine(result);

Console.Read();

}

}
復制代碼

4、使用回調方式返回結果

要注意的是“my.BeginInvoke(3,300, MethodCompleted, my)”,BeginInvoke方法的參數傳遞方式:

前面一部分(3,300)是其委托本身的參數。

倒數第二個參數(MethodCompleted)是回調方法委托類型,他是回調方法的委托,此委托沒有返回值,有一個IAsyncResult類型的參數,當method方法執行完后,系統會自動調用MethodCompleted方法。

最后一個參數(my)需要向MethodCompleted方法中傳遞一些值,一般可以傳遞被調用方法的委托,這個值可以使用IAsyncResult.AsyncState屬性獲得。

復制代碼
class Program
{

private delegate int MyMethod(int second, int millisecond);

//線程執行方法

private static int method(int second, int millisecond)

{

Console.WriteLine("線程休眠" + (second * 1000 + millisecond) + "毫秒");

Thread.Sleep(second * 1000 + millisecond);

Random random = new Random();

return random.Next(10000);

}

//回調方法

private static void MethodCompleted(IAsyncResult asyncResult)

{

if (asyncResult == null || asyncResult.AsyncState == null)

{

Console.WriteLine("回調失敗!!!");

return;

}

int result = (asyncResult.AsyncState as MyMethod).EndInvoke(asyncResult);

Console.WriteLine("任務完成,結果:" + result);

}

static void Main(string[] args)

{

MyMethod my = method;

IAsyncResult asyncResult = my.BeginInvoke(3,300, MethodCompleted, my);

Console.WriteLine("任務開始");

Console.Read();

}

}
5、其他組件的BeginInvoke和EndInvoke方法

在其他的.net組件中也有類似BeginInvoke和EndInvoke的方法,如System.Net.HttpWebRequest類的BeginGetResponse和EndGetResponse方法。其使用方法類似於委托類型的BeginInvoke和EndInvoke方法,例如:

class Program
{

//回調函數

private static void requestCompleted(IAsyncResult asyncResult)

{

if (asyncResult == null || asyncResult.AsyncState==null)

{

Console.WriteLine("回調失敗");

return;

}

HttpWebRequest hwr = asyncResult.AsyncState as HttpWebRequest;

HttpWebResponse response = (HttpWebResponse)hwr.EndGetResponse(asyncResult);

StreamReader sr = new StreamReader(response.GetResponseStream());

string str = sr.ReadToEnd();

Console.WriteLine("返回流長度:"+str.Length);

}

static void Main(string[] args)

{

HttpWebRequest request =

(HttpWebRequest)WebRequest.Create("http://www.baidu.com");

//異步請求

IAsyncResult asyncResult = request.BeginGetResponse(requestCompleted, request);

Console.WriteLine("任務開始");

Console.Read();

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM