標籤:
一、建立和啟動線程簡單說明
一個NSThread對象就代表一條線程
建立、啟動線程
(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
// 線程一啟動,就會線上程thread中執行self的run方法
主線程相關用法
+ (NSThread *)mainThread; // 獲得主線程
- (BOOL)isMainThread; // 是否為主線程
+ (BOOL)isMainThread; // 是否為主線程
其他用法
獲得當前線程
NSThread *current = [NSThread currentThread];
線程的調度優先順序:調度優先順序的取值範圍是0.0 ~ 1.0,預設0.5,值越大,優先順序越高
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
設定線程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
其他建立線程的方式
(2)建立線程後自動啟動線程
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隱式建立並啟動線程
[self performSelectorInBackground:@selector(run) withObject:nil];
上述2種建立線程方式的優缺點
優點:簡單快捷
缺點:無法對線程進行更詳細的設定
二、程式碼範例
1.使用古老的方式建立
1 // 2 // YYViewController.m 3 // 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 10 #import "YYViewController.h"11 #import <pthread.h>12 13 14 @interface YYViewController ()15 - (IBAction)btnClick;16 @end17 18 19 @implementation YYViewController20 21 22 - (void)viewDidLoad23 {24 [super viewDidLoad];25 }26 27 28 //按鈕的點擊事件29 - (IBAction)btnClick {30 //1.擷取當前線程31 NSThread *current=[NSThread currentThread];32 //主線程33 NSLog(@"btnClick----%@",current); 34 35 //2.使用for迴圈執行一些耗時操作36 pthread_t thread;37 pthread_create(&thread, NULL, run, NULL);38 }39 40 41 //c語言函數42 void *run(void *data)43 {44 //擷取當前線程,是新建立出來的線程45 NSThread *current=[NSThread currentThread];46 47 48 for (int i=0; i<10000; i++) {49 NSLog(@"btnClick---%d---%@",i,current);50 }51 return NULL;52 }53 54 //多個線程,點擊按鈕執行按鈕調用方法的時候,主線程沒有被阻塞55 56 @end
實現效果:
列印結果:
2.使用NSThread建立線程
1 // 2 // YYViewController.m 3 // 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h"10 #import <pthread.h>11 12 13 @interface YYViewController ()14 - (IBAction)btnClick;15 @end16 17 18 @implementation YYViewController 19 20 - (void)viewDidLoad21 {22 [super viewDidLoad];23 }24 25 26 //按鈕的點擊事件27 - (IBAction)btnClick {28 //1.擷取當前線程29 NSThread *current=[NSThread currentThread];30 //主線程31 NSLog(@"btnClick----%@",current);32 33 //擷取主線程的另外一種方式34 NSThread *main=[NSThread mainThread];35 NSLog(@"主線程-------%@",main);36 37 //2.執行一些耗時操作38 [self creatNSThread];39 // [self creatNSThread2];40 // [self creatNSThread3];41 }42 43 44 /**45 * NSThread建立線程方式146 * 1> 先建立初始化線程47 * 2> start開啟線程48 */49 -(void)creatNSThread50 {51 NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"線程A"];52 //為線程設定一個名稱53 thread.name=@"線程A";54 //開啟線程55 [thread start];56 57 58 NSThread *thread2=[[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"線程B"];59 //為線程設定一個名稱60 thread2.name=@"線程B";61 //開啟線程62 [thread2 start];63 }64 65 66 /**67 * NSThread建立線程方式268 *建立完線程直接(自動)啟動69 */70 -(void)creatNSThread271 {72 // NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];73 74 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];75 }76 77 78 /**79 * NSThread建立線程方式380 * 隱式建立線程, 並且直接(自動)啟動81 */82 -(void)creatNSThread383 {84 //在後台線程中執行===在子線程中執行85 [self performSelectorInBackground:@selector(run:) withObject:@"隱式建立"];86 }87 88 89 90 -(void)run:(NSString *)str91 {92 //擷取當前線程93 NSThread *current=[NSThread currentThread];94 //列印輸出95 for (int i=0; i<10; i++) {96 NSLog(@"run---%@---%@",current,str);97 }98 }99 @end
調用線程1,列印結果為:
調用線程2
調用線程3
iOS開發多線程篇—建立線程