ios 開發中的多執行緒

2021-06-21 17:17:27 字數 885 閱讀 2960

dispatch_async(dispatch_queue_create("com.enormego.egoimageloader",null),^);

});(二)nsoperation和nsopertionqueue

1,乙個繼承自nsoperation的操作類,該類的實現中必須有 (void) main()方法

2,最簡單的方法,將nsoperation的例項放入nsopertionqueue中

3,可以在nsopertionqueue中設定同時可以進行的運算元

(三)nsthread

1,detachnewthreadselector此為簡便方法,不用進行執行緒清理

[nsthread detachnewthreadselector:@selector(mythreadmainmethod:)totarget:self withobject:nil];

2,nsthread initialwithtarget,(void)start;方法,可以建立執行緒,但選擇合適的時機啟動執行緒

nsthread* mythread = [[nsthread alloc] initwithtarget:self

selector:@selector(mythreadmainmethod:)

object:nil];

[mythread start];

(四)執行緒間同步

1,原子鎖屬性,automic,noautomic,變數可以保證多執行緒訪問

2,nscondition,可以提供帶條件的同步鎖,解鎖時,需要輸入相同的條件才能解鎖

3,nslock, lock,unlock比較簡單的同步鎖

4,@synchronized(anobject)更簡單的所方式,通常用在單例物件的建立上面

**  :

IOS開發中多執行緒的使用

一 建立多執行緒的五種方式 nsthread thread nsthread alloc initwithtarget self selector selector update object nil nsthread detachnewthreadselector selector update ...

IOS開發中多執行緒的使用

一 建立多執行緒的五種方式 nsthread thread nsthread alloc initwithtarget self selector selector update object nil nsthread detachnewthreadselector selector update ...

iOS開發多執行緒 執行緒安全

資源共享 1塊資源可能會被多個執行緒共享,也就是多個執行緒可能會訪問同一塊資源 比如多個執行緒訪問同乙個物件 同乙個變數 同乙個檔案 當多個執行緒訪問同一塊資源時,很容易引發資料錯亂和資料安全問題 示例一 示例二 問題 import viewcontroller.h inte ce viewcont...