ios多執行緒之NSThread總結

2021-07-11 02:21:08 字數 977 閱讀 2479

因為不常用,所以nsthread需要掌握的沒有多少,會這些就夠了:

1.建立方式:

-------

(1)建立後啟動

nsthread *thread=[[nsthread alloc] initwithtarget:self selector:@selector(download:) object:@"先建立,後啟動"];

[thread start];

(2)建立後立即啟動

[nsthread detachnewthreadselector:@selector(download:) totarget:self withobject:@"建立自動啟動"];

(3)隱式啟動

[self performselectorinbackground:@selector(download:) withobject:@"隱式建立"];

2.常用方法:

-------

(1)獲得主線程:

[nsthread mainthread];

(2)獲得當前執行緒:

[nsthread currentthread];

(3)設定執行緒名字(除錯用):

[[nsthread currentthread] setname:@"thread1"];

(4)nsthread互斥鎖 保證原子 盡量不用:

@synchronized(self)

[self performselectoronmainthread:@selector(setimageafterdownload:) withobject:image waituntildone:yes];

或者要設定imageview的時候可以這樣:[self

.imageview performselectoronmainthread:@selector(setimage:) withobject:image waituntildone:yes];

iOS多執行緒之NSThread

ios多執行緒之nsthread 乙個nsthread物件就代表一條執行緒 1.建立,啟動執行緒 nsthread thread nsthread alloc initwithtarget self selector selector run object nil thread start 主線程相...

iOS多執行緒之NSThread

1.建立和啟動執行緒 nsthread thread nsthread alloc initwithtarget self selector selector run object nil thread start nsthread mainthread 獲得主線程 bool ismainthrea...

多執行緒之NSThread

nsthread thread nsthread alloc initwithtarget self selector selector run object nil 啟動執行緒 執行完畢會自動銷毀執行緒 thread start 主動銷毀執行緒 還未執行完畢就退出 thread exit 建立執行...