多執行緒之NSThread

2021-08-22 04:55:57 字數 1039 閱讀 1310

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

// 啟動執行緒(執行完畢會自動銷毀執行緒)

[thread start];

// 主動銷毀執行緒(還未執行完畢就退出)

[thread exit];

// 建立執行緒,建立完畢,即刻啟動 / detach : 分支 (需要傳參,所以run:)

// 隱式建立執行緒

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

// 輸出當前執行的執行緒

nslog(@"%@",[nsthread currentthread]);

// 獲取主線程

nsthread *thread = [nsthread mainthread];

// 獲取當前執行緒

nsthread *thread = [nsthread currentthread];

// 例項方法判斷目標執行緒是否為主執行緒

[thread ismainthread];

// 類方法判斷當前執行的**是否在主線程中

[nsthread ismainthread];

// 給執行緒設定名字

[thread setname:@"第二線程"];

// 獲取執行緒的名字

[thread name];

// 當前執行緒進入sleep狀態2s

[nsthread sleepfortimeinterval:2];

執行緒鎖 (小括號內的是物件 , 只要是物件且物件唯一 , 就可以作為唯一鎖 ,不是唯一鎖,沒有 )

@synchronized(self)

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...

ios多執行緒之NSThread總結

因為不常用,所以nsthread需要掌握的沒有多少,會這些就夠了 1.建立方式 1 建立後啟動 nsthread thread nsthread alloc initwithtarget self selector selector download object 先建立,後啟動 thread st...