zoukankan      html  css  js  c++  java
  • IOS多线程编程

    1:首先简单介绍什么叫线程

    • 可并发执行的,拥有最小系统资源,共享进程资源的基本调度单位。
    • 共用堆,自有栈(官方资料说明iOS主线程栈大小为1M,其它线程为512K)。
    • 并发执行进度不可控,对非原子操作易造成状态不一致,加锁控制又有死锁的风险。

    2:IOS中的线程

    • iOS主线程(UI线程),我们的大部分业务逻辑代码运行于主线程中。
    • 没有特殊需求,不应引入线程增加程序复杂度。
    • 应用场景:逻辑执行时间过长,严重影响交互体验(界面卡死)等。
    • IOS 多线程 有三种主要方法
      1)NSThread
      (2)NSOperation
      (3)GCD

      下面简单介绍这三个方法 

      1.NSThread

          调用方法如下: 

          如函数需要输入参数,可从object传进去。
          (1) [NSThread detachNewThreadSelector:@selector(threadInMainMethod:) toTarget:self withObject:nil];
          (2) NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadInMainMethod:) object:nil];
       [myThread start]; 
          (3) [obj performSelectorInBackground:@selector(threadMe) withObject:nil];

      提个问题:如果某个ViewController里运行了一个Thread,Thread还没结束的时候,这个ViewController被Release了,结果会如何? 

      经过的的测试,Thread不结束,ViewController一直保留,不会执行dealloc方法。

      2.NSOperation  

      NSoperation也是多线程的一种,NSopertaion有2种形式
        (1) 并发执行
             并发执行你需要重载如下4个方法
           //执行任务主函数,线程运行的入口函数
          - (void)start 
             //是否允许并发,返回YES,允许并发,返回NO不允许。默认返回NO
          -(BOOL)isConcurrent 
          - (BOOL)isExecuting
           //是否已经完成,这个必须要重载,不然放在放在NSOperationQueue里的NSOpertaion不能正常释放。
         - (BOOL)isFinished
         
         比如TestNSOperation:NSoperaion 重载上述的4个方法,
         声明一个NSOperationQueue, NSOperationQueue *queue = [[[NSOperationQueue alloc ] init] autorelease];
        [queue addOperation:testNSoperation];

        它会自动调用TestNSOperation里的 start函数,如果需要多个NSOperation,你需要设置queue的一些属性,如果多个NSOperation之间又依赖关系,也可以设置,具体可以参考API 文档。 

      (2)非并发执行
        -(void)main
         只需要重载这个main方法就可以了。  

        3.GCD

        GCD很强大,我的使用经验很少。但是scorpiozj 总结的比较全面(http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html

        同时,这篇文章也介绍的比较详细 http://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html 

           官方教程

      GCD是和block紧密相连的,所以最好先了解下block(可以查看这里).GCD是C level的函数,这意味着它也提供了C的函数指针作为参数,方便了C程序员.

      下面首先来看GCD的使用:

      dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

      async表明异步运行,block代表的是你要做的事情,queue则是你把任务交给谁来处理了.(除了async,还有sync,delay,本文以async为例).

      之所以程序中会用到多线程是因为程序往往会需要读取数据,然后更新UI.为了良好的用户体验,读取数据的操作会倾向于在后台运行,这样以避免阻塞主线程.GCD里就有三种queue来处理.

      1. Main queue:

        顾名思义,运行在主线程,由dispatch_get_main_queue获得.和ui相关的就要使用Main Queue.

      2.Serial quque(private dispatch queue)

        每次运行一个任务,可以添加多个,执行次序FIFO(队列,先进先出first input first out). 通常是指程序员生成的,比如:

      dispatch_queue_t myCustomQueue = dispatch_queue_create("example.MyCustomQueue", NULL);
      dispatch_async(myCustomQueue, ^{
          for (int abc=0;abc<100;abc++)
              {
                  printf("1.Do some work here.\n");
              }
          });
      dispatch_async(myCustomQueue, ^{
          for (int abc=0;abc<100;abc++)
              {
                  printf("2.Do some work here.\n");    
              }
          });
      dispatch_queue_t myCustomQueue2 = dispatch_queue_create("example.MyCustomQueue2", NULL);
      dispatch_async(myCustomQueue2, ^{
          for (int abc=0;abc<100;abc++)
              {
                  printf("1. myCustomQueue2 Do some work here.\n");
              }
          });
      dispatch_async(myCustomQueue2, ^{
          for (int abc=0;abc<100;abc++)
              {
                  printf("2. myCustomQueue2 Do some work here.\n");
              }
          });
      打印的结果必定会是 :然而,因为myCustomQueue 和 myCustomQueue2 是在两个队列中,所以在队列myCustomQueue中:
      1.Do some work here.” 在 “2.Do some work here.” 之前,而在myCustomQueue2
      队列中:“1. myCustomQueue2
      Do some work here.
      ”在“2. myCustomQueue2 Do some work here.”之前。而在myCustomQueue和myCustomQueue2
      之中的任务是没有先后的。及不是并发的。

      3. Concurrent queue(global dispatch queue):

      可以同时运行多个任务,每个任务的启动时间是按照加入queue的顺序,结束的顺序依赖各自的任务.使用dispatch_get_global_queue获得.

      所以我们可以大致了解使用GCD的框架:

      dispatch_queue_t newThread = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
      dispatch_async(newThread,^{[self downloadImage:ImageURL]; 
      });
      小节:NSThread的方式或许能做更快的切换,因为ARMv6或更高版本的处理器都提供了非常强大的线程切换机制。但是NSThread不会
      采取多核的分派,
      因为这个系统接口首先要保证的是用户线程的可靠性。 而Grand Central Dispatch显式地利用分派队列来做多核
      分派调度,因此如果是在多核处理器上的话用G_C_D更快。如果你的处理器是单核心的话,那么可以使用切换更快的NSThread。

      Where do I find GCD?

      1. GCD is part of libSystem.dylib
      2. #include <dispatch/dispatch.h>


  • 相关阅读:
    树莓派_GPIO
    Python_PyQt_基本使用
    python_静态,组合,继承
    Linux_操作
    Pyqt5+eric6安装教程
    树莓派操作
    python_爬虫_requests
    HTML的基本操作
    python_pandas_numpy_json
    python_模块_sys_time_random_os
  • 原文地址:https://www.cnblogs.com/lyanet/p/2856384.html
Copyright © 2011-2022 走看看