IOS開發中多執行緒的使用

2021-07-01 23:44:27 字數 1525 閱讀 5513

一、建立多執行緒的五種方式

nsthread * thread=[[nsthread alloc] initwithtarget:self selector:@selector(_update) object:nil];

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

[self performselectorinbackground:@selector(_update) withobject:nil];

nsoperationqueue *queue=[[nsoperationqueue alloc] init];

[queue addoperationwithblock:^

}];

//第一步開啟執行緒池

nsoperationqueue * queue=[[nsoperationqueue alloc] init];

//設定併發數目

[queue setmaxconcurrentoperationcount:2];

//第二部建立多執行緒新增到執行緒池

nsinvocationoperation * thread1=[[nsinvocationoperation alloc] initwithtarget:self selector:@selector(_update1) object:nil];

nsinvocationoperation *thread2=[[nsinvocationoperation alloc] initwithtarget:self selector:@selector(_update2) object:nil];

[thread1 setqueuepriority:nsoperationqueuepriorityverylow];

[thread2 setqueuepriority:nsoperationqueuepriorityveryhigh];

[queue addoperation:thread1];

[queue addoperation:thread2];

二、多執行緒應用例項,載入。

考慮到如果載入網路會延遲,在乙個主線程載入會影響控制項的渲染,此時可以採取多執行緒,非同步載入完成後重新整理ui。

主要**:

#import "uiimageview+thread.h"

@implementation uiimageview(load)

- (void) setimagewithurl:(nsstring *)url

- (void) _loadimage:(nsstring *)u

}

IOS開發中多執行緒的使用

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

ios 開發中的多執行緒

dispatch async dispatch queue create com.enormego.egoimageloader null 二 nsoperation和nsopertionqueue 1,乙個繼承自nsoperation的操作類,該類的實現中必須有 void main 方法 2,最簡...

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

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