1.NSThread創建方式(一個NSThread對象就代表一條線程)
1.1>創建/啟動線程
(1)線程一啟動,就會在thread中執行self的run方法
NSTread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
(2)創建線程后自動啟動線程
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隱式創建并啟動線程
[self performSelectorInBackground:@selector(run) withObject:nil];
//(2),(3)兩種創建線程方式,優點是簡單快捷,缺點是無法對線程進行更詳細的設置
1.2>主線程的相關用法
+(NSThread *)mainThread; //獲得主線程
(BOOL)isMainThread; //是否為主線程
(BOOL)isMainThread;//是否為主線程(類方法)
1.3>其他用法
NSThread *current = [NSThread currentThread]; //獲得當前線程
線程的調度優先級
(double) threadPRiority;
(BOOL)setThreadPriority:(double)p;
//調度優先級的取值范圍是0.0 ~ 1.0,默認是0.5,值越大,優先級越高
線程的名字:
-(void)setName:(NSString *)name;
-(NSString *)name;
新聞熱點
疑難解答