iOS開發多線程篇—建立線程
一、建立和啟動線程簡單說明
一個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 @end
17
18
19 @implementation YYViewController
20
21
22 - (void)viewDidLoad
23 {
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
57
58
實作效果:

列印結果:
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 @end
16
17
18 @implementation YYViewController
19
20 - (void)viewDidLoad
21 {
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建立線程方式1
46 * 1> 先建立初始化線程
47 * 2> start開啟線程
48 */
49 -(void)creatNSThread
50 {
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建立線程方式2
68 *建立完線程直接(自動)啟動
69 */
70
71 -(void)creatNSThread2
72 {
73 // NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];
74
75 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];
76 }
77
78
79 /**
80 * NSThread建立線程方式3
81 * 隐式建立線程, 并且直接(自動)啟動
82 */
83
84 -(void)creatNSThread3
85 {
86 //在背景線程中執行===在子線程中執行
87 [self performSelectorInBackground:@selector(run:) withObject:@"隐式建立"];
88 }
89
90
91
92 -(void)run:(NSString *)str
93 {
94 //擷取目前線程
95 NSThread *current=[NSThread currentThread];
96 //列印輸出
97 for (int i=0; i<10; i++) {
98 NSLog(@"run---%@---%@",current,str);
99 }
100 }
101 @end
調用線程1,列印結果為:
調用線程2
調用線程3