浅谈多线程——NSThread

  上一篇文章中我们大致了解了GCD的模式和方法,在iOS开发中除了GCD之外,还有NSThread和NSOperation两种多线程方式。

  1.NSThread

  - a - 使用NSThread开辟多线程进行子任务处理:类方法和初始化方法

  使用类方法不需要创建对象就可以直接开辟多线程并发;而创建NSThread对象进行开辟则需要使用 - (void)start 方法进行线程启动。

 1 #import "ViewController.h"
 2 
 3 typedef NS_ENUM(NSInteger, ENSThreadType) {
 4     kNSThreadClassFunc,
 5     kNSThreadInitFunc,
 6 };
 7 
 8 @interface ViewController ()
 9 
10 @end
11 
12 @implementation ViewController
13 
14 - (void)viewDidLoad {
15     [super viewDidLoad];
16     
17     [self threadWithType:kNSThreadInitFunc times:10];
18     
19 }
20 
21 - (void)threadWithType:(ENSThreadType)type times:(int)time{
22     for(int i = 0; i < time; i++){
23         NSString *str = [NSString stringWithFormat:@"测试+%d", i];
24         
25         switch (type) {
26             case kNSThreadClassFunc:{
27                 // ①类方法
28                 [NSThread detachNewThreadSelector:@selector(func:) toTarget:self withObject:str];
29             }
30                 break;
31             case kNSThreadInitFunc:{
32                 // ②初始化方法
33                 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(func:) object:str];
34                 thread.name = str;
35                 [thread start];
36             }
37                 break;
38             default:
39                 break;
40         }
41         
42     }
43 }
44 
45 - (void)func:(NSString*)str{
46     NSLog(@"%@ %@", str, [NSThread currentThread]);
47 }
48 
49 @end

  

  - b - NSThread实例方法,创建了NSThread对象后,可以调用它的实例方法:

 1 #import "ViewController.h"
 2 
 3 typedef NS_ENUM(NSInteger, ENSThreadObjFunc) {
 4     // kThreadObjFuncThread,
 5     kThreadObjFuncMain,
 6     kThreadObjFuncBackground,
 7 };
 8 
 9 @interface ViewController ()
10 {
11     NSThread *aThread;
12 }
13 @end
14 
15 @implementation ViewController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     
20     aThread = [[NSThread alloc] initWithTarget:self selector:@selector(func:) object:@"测试参考线程"];
21     [aThread start];
22     
23     [self threadObjFuncWithType:kThreadObjFuncMain thread:aThread times:10];
24     
25 }
26 
27 - (void)func:(NSString*)str{
28     NSLog(@"%@ %@", str, [NSThread currentThread]);
29 }
30 
31 - (void)threadObjFuncWithType:(ENSThreadObjFunc)type thread:(NSThread*)thread times:(int)time{
32     
33     for(int i = 0; i < time; i ++){
34         switch (type) {
35             case kThreadObjFuncMain:{
36                 // ①主线程队列中执行,同步
37                 NSString *str = [NSString stringWithFormat:@"主线程队列+%d", i];
38                 [self performSelectorOnMainThread:@selector(func:) withObject:str waitUntilDone:YES];
39             }
40                 break;
41             case kThreadObjFuncBackground:{
42                 // ②后台执行,并行异步
43                 NSString *str = [NSString stringWithFormat:@"后台执行+%d", i];
44                 [self performSelectorInBackground:@selector(func:) withObject:str];
45             }
46                 break;
47             // case kThreadObjFuncThread:{
48                 // ③在某一线程队列执行,类似于串行异步
49                 // NSString *str = [NSString stringWithFormat:@"方法+%d", i];
50                 // [self performSelector:@selector(func:) onThread:thread withObject:str waitUntilDone:YES];
51             // }
52                 // break;
53             default:
54                 break;
55         }
56     }
57 }
58 
59 @end

 

转载于:https://www.cnblogs.com/kriskee/p/5368155.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值