iOS开发中多线程基础

耗时操作演练

代码演练

  • 编写耗时方法
- (void)longOperation {
    for (int i = 0; i < 10000; ++i) {
        NSLog(@"%@ %d", [NSThread currentThread], i);
    }
}
  • 直接调用耗时方法
// 1> 直接调用耗时方法
[self longOperation];

运行测试效果

  • 在后台执行耗时方法
// 2> 在后台执行耗时方法
[self performSelectorInBackground:@selector(longOperation) withObject:nil];

运行测试效果

小结

  1. [NSThread currentThread]:当前线程对象
    • 可以在所有的多线程技术中使用!
    • 通常用来在多线程开发中,Log 代码是否在主线程运行
  2. number
    • number == 1 主线程
    • number != 1 后台线程
    • 不要纠结 number 的具体数字

pthread演练

  • pthreadPOSIX 多线程开发框架,由于是跨平台的 C 语言框架,在苹果的头文件中并没有详细的注释
  • 要查阅 pthread 有关资料,可以访问 http://baike.baidu.com

导入头文件

#import <pthread.h>

pthread演练

// 创建线程,并且在线程中执行 demo 函数
- (void)pthreadDemo {

    /**
     参数:
     1> 指向线程标识符的指针,C 语言中类型的结尾通常 _t/Ref,而且不需要使用 *
     2> 用来设置线程属性
     3> 线程运行函数的起始地址
     4> 运行函数的参数

     返回值:
     - 若线程创建成功,则返回0
     - 若线程创建失败,则返回出错编号


     */
    pthread_t threadId = NULL;
    NSString *str = @"Hello Pthread";
    int result = pthread_create(&threadId, NULL, demo, (__bridge void *)(str));

    if (result == 0) {
        NSLog(@"创建线程 OK");
    } else {
        NSLog(@"创建线程失败 %d", result);
    }
}

// 后台线程调用函数
void *demo(void *params) {
    NSString *str = (__bridge NSString *)(params);

    NSLog(@"%@ - %@", [NSThread currentThread], str);

    return NULL;
}

小结

  1. 在 C 语言中,没有对象的概念,对象是以结构体的方式来实现的
  2. 通常,在 C 语言框架中,对象类型以 _t/Ref 结尾,而且声明时不需要使用 *
  3. C 语言中的 void * 和 OC 中的 id 是等价的
  4. 内存管理
    • 在 OC 中,如果是 ARC 开发,编译器会在编译时,根据代码结构,自动添加 retain/release/autorelease
    • 但是,ARC 只负责管理 OC 部分的内存管理,而不负责 C 语言 代码的内存管理
    • 因此,开发过程中,如果使用的 C 语言框架出现 retain/create/copy/new 等字样的函数,大多都需要 release,否则会出现内存泄漏
  5. 在混合开发时,如果在 COC 之间传递数据,需要使用 __bridge 进行桥接,桥接的目的就是为了告诉编译器如何管理内存
  6. 桥接的添加可以借助 Xcode 的辅助功能添加
  7. MRC 中不需要使用桥接

三种创建线程的方法

准备函数

// MARK: - 后台线程调用函数
- (void)longOperation:(id)obj {
    NSLog(@"%@ - %@", [NSThread currentThread], obj);
}

1. alloc / init - start

// MARK: - NSThread 演练
- (void)threadDemo1 {
    // 1. 实例化线程对象 => alloc(分配内存) / init(初始化)
    NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector(longOperation:) object:@"alloc/init"];

    // 2. 启动线程
    [t start];

    // 3. 当前线程?
    NSLog(@"%@", [NSThread currentThread]);
}

演练小结

  1. [t start];执行后,会在另外一个线程执行 demo 方法
  2. 在 OC 中,任何一个方法的代码都是从上向下顺序执行的
  3. 同一个方法内的代码,都是在相同线程执行的(block除外)

2. detachNewThreadSelector

- (void)threadDemo2 {
    // detach => 分离一个子线程执行 demo: 方法
    [NSThread detachNewThreadSelector:@selector(longOperation:) toTarget:self withObject:@"Detach"];

    // 2. 当前线程?
    NSLog(@"%@", [NSThread currentThread]);
}

演练小结

  • detachNewThreadSelector 类方法不需要启动,创建线程后自动启动线程执行 @selector 方法

3. 分类方法

- (void)threadDemo3 {
    // 1. 在后台执行 @selector 方法
    [self performSelectorInBackground:@selector(longOperation:) withObject:@"category"];

    // 2. 当前线程?
    NSLog(@"%@", [NSThread currentThread]);
}
  1. performSelectorInBackgroundNSObject 的分类方法
  2. 没有 thread 字眼,会立即在后台线程执行 @selector 方法
  3. 所有 NSObject 都可以使用此方法,在其他线程执行方法!

自定义对象

Person 类

// MARK: - Person 类
@interface Person : NSObject
/// 姓名
@property (nonatomic, copy) NSString *name;
@end

@implementation Person

/// 使用字典实例化对象
+ (instancetype)personWithDict:(NSDictionary *)dict {
    Person *p = [[Person alloc] init];

    [p setValuesForKeysWithDictionary:dict];

    return p;
}

/// 加载数据
- (void)loadData {
    NSLog(@"加载数据 %@ %@", [NSThread currentThread], self.name);
}

@end

Person 类使用分类方法

- (void)threadDemo4 {
    Person * p = [Person personWithDict:@{@"name": @"zhangsan"}];

    [p performSelectorInBackground:@selector(loadData) withObject:nil];
}

线程状态

演练代码

// MARK: - 线程状态演练
- (void)statusDemo {

    NSLog(@"睡会");
    [NSThread sleepForTimeInterval:1.0];

    for (int i = 0; i < 20; ++i) {
        if (i == 8) {
            NSLog(@"再睡会");
            [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
        }

        NSLog(@"%@ %d", [NSThread currentThread], i);

        if (i == 10) {
            NSLog(@"88");
            [NSThread exit];
        }
    }
    NSLog(@"能来吗?");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 注意不要在主线程上调用 exit 方法
//    [NSThread exit];

    // 实例化线程对象(新建)
    NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector(statusDemo) object:nil];

    // 线程就绪(被添加到可调度线程池中)
    [t start];
}

阻塞

  • 方法执行过程,符合某一条件时,可以利用 sleep 方法让线程进入 阻塞 状态

1> sleepForTimeInterval

  • 从现在起睡多少

2> sleepUntilDate

  • 从现在起睡到指定的日期

死亡

[NSThread exit];

  • 一旦强行终止线程,后续的所有代码都不会被执行
  • 注意:在终止线程之前,应该注意释放之前分配的对象!

就绪 -> 运行

线程从就绪运行状态之间的切换是由 CPU 负责的,程序员无法干预

线程属性

演练代码

// MARK: - 线程属性
- (void)threadProperty {
    NSThread *t1 = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];

    // 1. 线程名称
    t1.name = @"Thread AAA";
    // 2. 优先级
    t1.threadPriority = 0;

    [t1 start];

    NSThread *t2 = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];

    // 1. 线程名称
    t2.name = @"Thread BBB";
    // 2. 优先级
    t2.threadPriority = 1;

    [t2 start];
}

- (void)demo {
    for (int i = 0; i < 10; ++i) {
        // 堆栈大小
        NSLog(@"%@ 堆栈大小:%tuK", [NSThread currentThread], [NSThread currentThread].stackSize / 1024);
    }

    // 模拟崩溃
    // 判断是否是主线程
//    if (![NSThread currentThread].isMainThread) {
//        NSMutableArray *a = [NSMutableArray array];
//
//        [a addObject:nil];
//    }
}

属性

1. name - 线程名称

  • 在大的商业项目中,通常需要在程序崩溃时,获取程序准确执行所在的线程

2. threadPriority - 线程优先级

  • 优先级,是一个浮点数,取值范围从 0~1.0
    • 1.0表示优先级最高
    • 0.0表示优先级最低
    • 默认优先级是0.5
  • 优先级高只是保证 CPU 调度的可能性会高
  • 刀哥个人建议,在开发的时候,不要修改优先级
  • 多线程的目的:是将耗时的操作放在后台,不阻塞主线程和用户的交互!
  • 多线程开发的原则:简单

3. stackSize - 栈区大小

  • 默认情况下,无论是主线程还是子线程,栈区大小都是 512K
  • 栈区大小可以设置
[NSThread currentThread].stackSize = 1024 * 1024;

4. isMainThread - 是否主线程

资源共享-卖票

多线程开发的复杂度相对较高,在开发时可以按照以下套路编写代码:

  1. 首先确保单个线程执行正确
  2. 添加线程

卖票逻辑

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.tickets = 20;

    [self saleTickets];
}

/// 卖票逻辑 - 每一个售票逻辑(窗口)应该把所有的票卖完
- (void)saleTickets {
    while (YES) {
        if (self.tickets > 0) {
            self.tickets--;
            NSLog(@"剩余票数 %d %@", self.tickets, [NSThread currentThread]);
        } else {
            NSLog(@"没票了 %@", [NSThread currentThread]);
            break;
        }
    }
}

添加线程

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.tickets = 20;

    NSThread *t1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil];
    t1.name = @"售票员 A";
    [t1 start];

    NSThread *t2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil];
    t2.name = @"售票员 B";
    [t2 start];
}

添加休眠

- (void)saleTickets {
    while (YES) {
        // 模拟休眠
        [NSThread sleepForTimeInterval:1.0];

        if (self.tickets > 0) {
            self.tickets--;
            NSLog(@"剩余票数 %d %@", self.tickets, [NSThread currentThread]);
        } else {
            NSLog(@"没票了 %@", [NSThread currentThread]);
            break;
        }
    }
}

运行测试结果

互斥锁

添加互斥锁

- (void)saleTickets {
    while (YES) {
        // 模拟休眠
        [NSThread sleepForTimeInterval:1.0];

        @synchronized(self) {
            if (self.tickets > 0) {
                self.tickets--;
                NSLog(@"剩余票数 %d %@", self.tickets, [NSThread currentThread]);
            } else {
                NSLog(@"没票了 %@", [NSThread currentThread]);
                break;
            }
        }
    }
}

互斥锁小结

  1. 保证锁内的代码,同一时间,只有一条线程能够执行!
  2. 互斥锁的锁定范围,应该尽量小,锁定范围越大,效率越差!
  3. 速记技巧 [[NSUserDefaults standardUserDefaults] synchronize];
互斥锁参数
  1. 能够加锁的任意 NSObject 对象
  2. 注意:锁对象一定要保证所有的线程都能够访问
  3. 如果代码中只有一个地方需要加锁,大多都使用 self,这样可以避免单独再创建一个锁对象

原子属性

  • 原子属性(线程安全),是针对多线程设计的,是默认属性
  • 多个线程在写入原子属性时(调用 setter 方法),能够保证同一时间只有一个线程执行写入操作
  • 原子属性是一种单(线程)写多(线程)读的多线程技术
  • 原子属性的效率比互斥锁高,不过可能会出现脏数据
  • 在定义属性时,必须显示地指定 nonatomic

演练代码

@interface ViewController ()
@property (atomic, strong) NSObject *obj1;
@property (atomic, strong) NSObject *obj2;
@end

@implementation ViewController
@synthesize obj1 = _obj1;

// 原子属性模拟代码
/// obj1 - getter
- (NSObject *)obj1 {
    return _obj1;
}

/// obj1 - setter
- (void)setObj1:(NSObject *)obj1 {
    @synchronized(self) {
        _obj1 = obj1;
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    long largeNumber = 1000 * 1000;

    // 互斥锁测试
    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    for (int i = 0; i < largeNumber; ++i) {
        self.obj1 = [[NSObject alloc] init];
    }
    NSLog(@"%f", CFAbsoluteTimeGetCurrent() - start);

    // 自旋锁测试
    start = CFAbsoluteTimeGetCurrent();
    for (int i = 0; i < largeNumber; ++i) {
        self.obj2 = [[NSObject alloc] init];
    }
    NSLog(@"%f", CFAbsoluteTimeGetCurrent() - start);
}

@end

原子属性内部的锁是自旋锁自旋锁的执行效率比互斥锁高

自旋锁 & 互斥锁

  • 共同点

    • 都能够保证同一时间,只有一条线程执行锁定范围的代码
  • 不同点

    • 互斥锁:如果发现有其他线程正在执行锁定的代码,线程会进入休眠状态,等待其他线程执行完毕,打开锁之后,线程会被唤醒
    • 自旋锁:如果发现有其他线程正在执行锁定的代码,线程会以死循环的方式,一直等待锁定代码执行完成
  • 结论

    • 自旋锁更适合执行非常短的代码
    • 无论什么锁,都是要付出代价

线程安全

  • 多个线程进行读写操作时,仍然能够得到正确结果,被称为线程安全
  • 要实现线程安全,必须要用到
  • 为了得到更佳的用户体验,UIKit 不是线程安全的

约定:所有更新 UI 的操作都必须主线程上执行!

  • 因此,主线程又被称为UI 线程

iOS 开发建议

  1. 所有属性都声明为 nonatomic
  2. 尽量避免多线程抢夺同一块资源
  3. 尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力

线程间通讯

主线程实现

定义属性

/// 根视图是滚动视图
@property (nonatomic, strong) UIScrollView *scrollView;
/// 图像视图
@property (nonatomic, weak) UIImageView *imageView;
/// 网络下载的图像
@property (nonatomic, weak) UIImage *image;

loadView

loadView 方法的作用:

  1. 加载视图层次结构
  2. 用纯代码开发应用程序时使用
  3. 功能和 Storyboard & XIB 是等价的

如果重写了 loadViewStoryboard & XIB 都无效

- (void)loadView {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor orangeColor];
    self.view = self.scrollView;

    UIImageView *iv = [[UIImageView alloc] init];
    [self.view addSubview:iv];
    self.imageView = iv;
}

viewDidLoad

  1. 视图加载完成后执行
  2. 可以做一些数据初始化的工作
  3. 如果用纯代码开发,不要在此方法中设置界面 UI
- (void)viewDidLoad {
    [super viewDidLoad];

    // 下载图像
    [self downloadImage];
}

下载网络图片

- (void)downloadImage {
    // 1. 网络图片资源路径
    NSURL *url = [NSURL URLWithString:@"http://c.hiphotos.baidu.com/image/pic/item/4afbfbedab64034f42b14da1aec379310a551d1c.jpg"];

    // 2. 从网络资源路径实例化二进制数据(网络访问)
    NSData *data = [NSData dataWithContentsOfURL:url];

    // 3. 将二进制数据转换成图像
    UIImage *image = [UIImage imageWithData:data];

    // 4. 设置图像
    self.image = image;
}

设置图片

- (void)setImage:(UIImage *)image {
    // 1. 设置图像视图的图像
    self.imageView.image = image;

    // 2. 按照图像大小设置图像视图的大小
    [self.imageView sizeToFit];

    // 3. 设置滚动视图的 contentSize
    self.scrollView.contentSize = image.size;
}

设置滚动视图的缩放

1> 设置滚动视图缩放属性

// 1> 最小缩放比例
self.scrollView.minimumZoomScale = 0.5;
// 2> 最大缩放比例
self.scrollView.maximumZoomScale = 2.0;
// 3> 设置代理
self.scrollView.delegate = self;

2> 实现代理方法 - 告诉滚动视图缩放哪一个视图

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

3> 跟踪 scrollView 缩放效果

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    NSLog(@"%@", NSStringFromCGAffineTransform(self.imageView.transform));
}

线程间通讯

  • 在后台线程下载图像
[self performSelectorInBackground:@selector(downloadImage) withObject:nil];
  • 在主线程设置图像
[self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值