zoukankan      html  css  js  c++  java
  • 多线程之Nsthread的使用方法

    一 NSThread的基本使用

    1)NSThread创建的四种方式

    第一种 创建方式 alloc initwith......

     特点:需要手动启动线程,可以拿到线程对象进行详细设置
      //创建线程  
       NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"wendingding"];
      //启动线程
      [thread start];
        第1个参数:目标对象
        第2个参数:选择器,线程启动要调用哪个方法
        第3个参数:前面方法要接受的参数(最多只能接受一个参数,没有就写nil)

    第二种创建线程的方式:分离出一条子线程

    特点:自动启动线程,无法对线程进行更详细的设置
     [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是分离出来的子线程"];
    第1个参数:线程启动调用的方法
    第2个参数:目标对象
    第3个参宿:传递给调用方法的参数

    第三种创建线程的方式: 后台创建

    特点:自动启动线程,无法进行更详细的设置
    [self performSelectorInBackground:@selector(run:) withObject:@"我是后台线程"];

    第四种 自定义
    需要自定义NSThread类重写内部方法实现

    设置线程的属性

    设置线程名称

    thread.name = @"线程A"

    设置线程的优先级,优先级的取值范围是0.0~1.0 ,1.0表示最高 在不设置状态下 默认是0.5

    thread.threadpriority = 1.0;

    二 线程状态

    线程的各种状态: 新建 就绪 运行 阻塞 死亡
    常用的控制线程状态的方法

    [NSThread exit]//退出当前线程
    [NSThread sleepForTimeInterval:2.0]//阻塞线程
    [NSThread sleepUntiDate:[NSDate          dateWithTimeIntervalSinceNow:2.0]]//阻塞线程
    注意线程死后不能复生

    三 线程安全

    01 前提:多个线程访问同一块资源会发生数据安全问题
    02 解决方案 :加互斥锁
    03 代码 : @synchronized(self){}
    04 专业术语: 线程同步
    05 原子核非原子属性 (是否对set方法加锁)

    四 线程间通信

    -(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
        {
        //    [self download2];
    //开启一条子线程来下载图片
    [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];
        }
    - (void)downioadImage
    {
            //1.确定要下载网络图片的url地址,一个url唯一对应着网络上的一个资源
            NSURL *url = [NSURL URLWithString:@"http://p6.qhimg.com/t01d2954e2799c461ab.jpg"];
    
            //2.根据url地址下载图片数据到本地(二进制数据
            NSData *data = [NSData dataWithContentsOfURL:url];
    
            //3.把下载到本地的二进制数据转换成图片
            UIImage *image = [UIImage imageWithData:data];
    
            //4.回到主线程刷新UI
            //4.1 第一种方式
        //    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
    
            //4.2 第二种方式
        //    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    
            //4.3 第三种方式
            [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
        }

    五 如何计算代码段的执行时间

     //第一种方法
            NSDate *start = [NSDate date];
            //2.根据url地址下载图片数据到本地(二进制数据)
            NSData *data = [NSData dataWithContentsOfURL:url];
    
            NSDate *end = [NSDate date];
            NSLog(@"第二步操作花费的时间为%f",[end timeIntervalSinceDate:start]);
    
     //第二种方法
            CFTimeInterval start = CFAbsoluteTimeGetCurrent();
            NSData *data = [NSData dataWithContentsOfURL:url];
    
            CFTimeInterval end = CFAbsoluteTimeGetCurrent();
            NSLog(@"第二步操作花费的时间为%f",end - start);



    文/放肆肆肆(简书作者)
    原文链接:http://www.jianshu.com/p/26d0adf9b7aa
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 相关阅读:
    Python-读取文件内容
    torch_03_二分类
    torch_02_多项式回归
    pytorch_01_基础_一维线性回归
    LC-KSVD 代码解读
    标签一致项(LC-KSVD)-全文解读
    PCA:主成分分析
    牛客刷题-大佬链接
    调试MATLAB代码
    scroll offset & client总结
  • 原文地址:https://www.cnblogs.com/zhufeng1994/p/5601457.html
Copyright © 2011-2022 走看看