zoukankan      html  css  js  c++  java
  • 关于多线程(NSThread介绍)

    通常在iOS中会遇到四种多线程编程的技术,分别是:
    (一)pthread
    (二)NSThread
    (三)NSOperation
    (四)GCD(全称:Grand Central Dispatch,又译为“牛逼的中枢调度器”)

    Snip20161209_2.png
    pthread其实不用多说,因为是C语言的,所以在OC中使用十分不便,几乎不用。NSThread这套方案是经过苹果封装后,并且完全面向对象的。所以你可以直接操控线程对象,非常直观和方便。不过它的生命周期还是需要我们手动管理,所以实际上使用也比较少,使用频率较多的是GCD以及NSOperation。
    下面先来介绍一下NSThread。

    NSThread 有四种直接创建方式:
    //方法一
    //优点:能拿到线程对象 缺点:需要手动的启动线程
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ios"];
    //02 启动线程
    [thread start];
    
    //方法二
    //优点:自动启动线程 缺点:不能拿到线程对象
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
    
    //方法三 开启一条后台线程
    //优点:自动启动线程 缺点:不能拿到线程对象
    [self performSelectorInBackground:@selector(run:) withObject:@"后台线程"];
    
    //方法四 自定义
    NewThread *threadB = [[NewThread alloc]init];
    [threadB start];
     //自定义的优点
    -(void)main
    {
        NSLog(@"重写main方法封装任务--%@",[NSThread currentThread]);
    }
    

    如果是先创建线程对象,然后再运行线程操作,在运行线程操作前可以设置线程的优先级等线程信息。 设置优先级 0~1.0 默认是0.5 最高是1.0。

     判断线程是否是主线程
    //1.number == 1 主线程 != 1 子线程
    //2.对象方法来判断某个线程是否是主线程
      NSThread *currentThread = [NSThread currentThread];
      NSLog(@"%zd",[currentThread isMainThread]);
    //3.判断当前线程是否是主线程
      NSLog(@"%zd",[NSThread isMainThread]);
    

    这里有个不得不提的知识点那就是线程的安全,那么也要提到一个关于卖票的经典案例和同步锁。

    首先定义100张票以及三个线程售票员
     self.totalCount = 100;
    
    //01 创建三个线程对象(售票员)
    self.thread01 = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread02 = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread03 = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];
    
    //02 设置名称
    self.thread01.name = @"售票员A";
    self.thread02.name = @"售票员B";
    self.thread03.name = @"售票员C";
    
    //03 启动线程
    [self.thread01 start];
    [self.thread02 start];
    [self.thread03 start];
    
    然后提供一个售票、计算剩余票数的方法
    -(void)saleTicket
    {
      while (1) {
            //检查余票
            NSInteger count = self.totalCount;
            if (count >0) {
                //卖出去一张
            self.totalCount = count - 1;
            NSLog(@"%@卖出一张票,还剩%zd张票",[NSThreadcurrentThread].name,self.totalCount);
        }else{
            NSLog(@"%@票已经卖完了",[NSThread currentThread].name);
            break;
            }
        }
    }
    

    Snip20161210_1.png
    通过观察打印结果可知,如果没有进行其他处理的话,多线程同时争夺同一块资源将会造成线程的不安全。在这里的直观表现为顺序的错乱,以及同一张票被售出多次的情况,所以这种情况下我们需要引入同步锁来保证线程安全。加锁方法如下:

    -(void)saleTicket
    {
        while (1) {
    
        //OC中的同步锁:(锁对象) + {要锁住的代码}
        //锁对象:要求是全局唯一的属性
        
       @synchronized (self) {
    
            //检查余票
            NSInteger count = self.totalCount;
            if (count >0) {
                //卖出去一张
                self.totalCount = count - 1;
                NSLog(@"%@卖出去了一张票还剩下%zd张票",[NSThread currentThread].name,self.totalCount);
            }else
            {
                NSLog(@"%@发现票已经卖完了",[NSThread currentThread].name);
                break;
            }
        }
      }
    }
    

    此处有两个注意点:1)要注意加锁的位置 2)加锁需要耗费性能,因此需要注意加锁的条件(多线程访问同一块资源)

    当然,NSThread还可以用来做线程间通讯,比如下载图片并展示为例,将下载耗时操作放在子线程,下载完成后再切换回主线程在UI界面对图片进行展示

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent     *)event
    {
        [NSThread detachNewThreadSelector:@selector(download) toTarget:self withObject:nil];
    }
    
    在download方法中进行线程切换并展示图片:
     [self.imageView performSelector:@selector(setImage:) onThread:    [NSThread mainThread] withObject:image waitUntilDone:YES];
    

    NSThread的基本介绍就到此结束,下面将会对带来GCD和NSOperation的个人理解分享。

  • 相关阅读:
    python使用zipfile递归压缩和解压缩文件
    文件上传控件bootstrap-fileinput中文设置没有效果的情况
    vue keep-alive 不生效和多级(三级以上)缓存失败
    Entityframework批量删除
    EasyUI ComboGrid 集成分页、按键示例
    在Entity Framework中使用事务
    MVC实用构架设计(三)——EF-Code First(6):数据更新最佳实践
    Entityframework更新数据和插入数据
    esayui-datagrid的使用
    javascript:;与javascript:void(0)使用介绍
  • 原文地址:https://www.cnblogs.com/guwudao/p/6153415.html
Copyright © 2011-2022 走看看