使用NSThread開辟線程有兩種方式:
(1)手動(dòng)開啟方式的創(chuàng)建:
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread) object:@"thread"];
[thread start];// 開啟線程
(2)自動(dòng)開啟方式的創(chuàng)建:
[NSThread detachNewThreadSelector:@selector(thread1:) toTarget:self withObject:@"thread1"];
- (void)thread1:(NSString *)sender{
[NSThread currentThread];//獲取到當(dāng)前所在的信息
NSThread *thread = [NSThread currentThread];
// thread.name = @"我是子線程 ";
// NSLog(@"%@",thread);
//// [NSThread isMainThread] 判斷當(dāng)前線程是否是主線程
// BOOL isMainThread = [NSThread isMainThread];
//// [NSThread isMultiThreaded] 判斷是否是多線程
// BOOL isMUltiThread = [NSThread isMultiThreaded];
// NSLog(@"%d察署,%d",isMainThread,isMUltiThread);
//// 設(shè)置線程的優(yōu)先級(jí)(0-1) setThreadPriority:
// [NSThread setThreadPriority:1.0];
//// sleepForTimeInterval:讓線程休眠
// [NSThread sleepForTimeInterval:2];
// 從網(wǎng)絡(luò)加載圖片并將它轉(zhuǎn)化為data類型的數(shù)據(jù)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
image = [UIImage imageWithData:data];
// waiUntilDone設(shè)為YES葛账,意味著UI更新完才會(huì)做其它操作
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}