iOS多线程 iOS开发Demo(示例程序)源代码

本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版)


  1. iOS程序源代码下载链接:
    01.大任务.zip
    225.8 KB
  2. //
  3. //  ViewController.m
  4. //  01.大任务
  5. //
  6. //  Created by apple on 13-12-27.
  7. //  Copyright (c) 2013itcast. All rights reserved.
  8. //

  9. #import"ViewController.h"

  10. @interfaceViewController()
  11. {
  12.     //全局的操作队列,由它统一控制所有的NSOperation的操作调度
  13.     NSOperationQueue        *_queue;
  14. }

  15. @property(weak,nonatomic)IBOutletUIImageView*imageView;

  16. @end

  17. @implementationViewController

  18. /**
  19.  无论使用哪种多线程技术都可以使用
  20.  [NSThread currentThread]跟踪查看当前执行所在的线程情况。
  21.  num = 1表示在主线程上执行的任务
  22.  
  23.  ================================================================
  24.  1. NSObject多线程技术
  25.  
  26.  1>使用performSelectorInBackground可以开启后台线程,执行selector选择器选择的方法
  27.  2>使用performSelectorOnMainThread可以重新回到主线程执行任务,通常用于后台线程更新界面UI时使用
  28.  3> [NSThread sleepForTimeInterval:1.0f];
  29.     让当前线程休眠,通常在程序开发中,用于模拟耗时操作,以便跟踪不同的并发执行情况!
  30.  
  31.     但是:在程序发布时,千万不要保留此方法!不要把测试中的代码交给客户,否则会造成不好的用户体验。
  32.  
  33.  提示:使用performSelectorInBackground也可以直接修改UI,但是强烈不建议使用。
  34.  
  35.  注意:在使用NSThread或者NSObject的线程方法时,一定要使用自动释放池,否则容易出现内存泄露。
  36.  
  37.  ================================================================
  38.  2. NSThread的多线程技术
  39.  
  40.  1>类方法直接开启后台线程,并执行选择器方法
  41.     detachNewThreadSelector
  42.  
  43.  2>成员方法,在实例化线程对象之后,需要使用start执行选择器方法
  44.     initWithTarget
  45.  
  46.  对于NSThread的简单使用,可以用NSObjectperformSelectorInBackground替代
  47.  
  48.  同时,在NSThread调用的方法中,同样要使用autoreleasepool进行内存管理,否则容易出现内存泄露。
  49.  //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
  50.  ================================================================
  51.  3. NSOperation,面向对象的多线程技术
  52.  
  53.  1>使用步骤:
  54.     1实例化操作
  55.         a) NSInvocationOperation
  56.         b) NSBlockOperation
  57.     2将操作添加到队列NSOperationQueue即可启动多线程执行
  58.  
  59.  2>更新UI使用主线程队列
  60.     [NSOpeationQueue mainQueue] addOperation ^{};
  61.  
  62.  3>操作队列的setMaxConcurrentOperationCount
  63.     可以设置同时并发的线程数量!
  64.  
  65.     提示:此功能仅有NSOperation有!
  66.  
  67.  4>使用addDependency可以设置任务的执行先后顺序,同时可以跨操作队列指定依赖关系
  68.  
  69.     提示:在指定依赖关系时,注意不要循环依赖,否则不工作。
  70.  //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
  71.  ================================================================
  72.  4. GCDC语言
  73.  
  74.  */
  75. - (void)viewDidLoad
  76. {
  77.     [superviewDidLoad];
  78.     
  79.     NSLog(@"%@", [NSThreadcurrentThread]);
  80.     
  81.     //实例化操作队列
  82.     _queue= [[NSOperationQueuealloc]init];
  83. }

  84. #pragma mark -操作
  85. //耗时操作演示
  86. - (void)bigDemo
  87. {
  88.     //自动释放池
  89.     //负责其他线程上的内存管理,在使用NSThread或者NSObject的线程方法时,一定要使用自动释放池
  90.     //否则容易出现内存泄露。
  91.     @autoreleasepool{
  92.         //    //模拟网络下载延时
  93.         //    for (NSInteger i = 0; i < 1000; i++) {
  94.         //        NSString *str = [NSString stringWithFormat:@"%d", i];
  95.         //
  96.         //        //提示:NSLog是非常耗时的操作!
  97.         //        NSLog(@"大任务-> %@", str);
  98.         //    }
  99.         
  100.         NSLog(@"%@", [NSThreadcurrentThread]);
  101.         //模拟网络下载延时,睡眠1秒,通常是在开发中测试使用。
  102.         [NSThreadsleepForTimeInterval:1.0f];
  103.         
  104.         //强烈不建议直接在后台线程更新界面UI
  105.         //模拟获取到下载的图像
  106.         UIImage*image = [UIImageimageNamed:@"头像1"];
  107.         
  108.         //在主线程更新图像
  109.         //使用self调用updateImage方法在主线程更新图像
  110.         //    [self performSelectorOnMainThread:@selector(updateImage:) withObject:image waitUntilDone:YES];
  111.         
  112.         //使用imageViewsetImage方法在主线程更新图像
  113.         [_imageViewperformSelectorOnMainThread:@selector(setImage:)withObject:imagewaitUntilDone:YES];
  114.         
  115.         // ....
  116.     }
  117. }

  118. #pragma mark -更新图像,模拟从网络上下载完图片后,更新界面的操作
  119. - (void)updateImage:(UIImage*)image
  120. {
  121.     NSLog(@"更新图像-> %@", [NSThreadcurrentThread]);
  122.     
  123.     _imageView.image= image;
  124. }

  125. #pragma mark - Actions
  126. - (IBAction)bigTask
  127. {
  128.     //本方法中的所有代码都是在主线程中执行的
  129.     // NSObject多线程技术
  130.     NSLog(@"执行前->%@", [NSThreadcurrentThread]);
  131.     
  132.     // performSelectorInBackground是将bigDemo的任务放在后台线程中执行
  133.     [selfperformSelectorInBackground:@selector(bigDemo)withObject:nil];
  134.     
  135.     NSLog(@"执行后->%@", [NSThreadcurrentThread]);
  136. //    [self bigDemo];
  137.     
  138.     NSLog(@"执行完毕");
  139. }

  140. - (IBAction)smallTask
  141. {
  142.     NSString*str =nil;
  143.     
  144.     for(NSIntegeri =0; i <50000; i++) {
  145.         str = [NSStringstringWithFormat:@"%d", i];
  146.     }
  147.     
  148.     NSLog(@"小任务-> %@", str);
  149. }

  150. #pragma mark NSThread演练
  151. - (IBAction)threadDemo
  152. {
  153.     //新建一个线程,调用@selector方法
  154. //    [NSThread detachNewThreadSelector:@selector(bigDemo) toTarget:self withObject:nil];
  155.     
  156.     //成员方法
  157.     NSThread*thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(bigDemo)object:nil];
  158.     
  159.     //启动start线程
  160.     [threadstart];
  161. }

  162. #pragma mark - NSOperation演练
  163. - (void)opAction
  164. {
  165.     NSLog(@"%@", [NSThreadcurrentThread]);
  166.     
  167.     //模拟延时
  168.     [NSThreadsleepForTimeInterval:1.0f];
  169.     
  170.     //模拟获取到图像
  171.     UIImage*image = [UIImageimageNamed:@"头像1"];
  172.     
  173.     //设置图像,在主线程队列中设置
  174.     [[NSOperationQueuemainQueue]addOperationWithBlock:^{
  175.         _imageView.image= image;
  176.     }];
  177. }

  178. #pragma mark invocation
  179. - (IBAction)operationDemo1
  180. {
  181.     NSInvocationOperation*op1 = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(opAction)object:nil];
  182.     
  183.     //如果使用start,会在当前线程启动操作
  184. //    [op1 start];
  185.     
  186.     // 1.一旦将操作添加到操作队列,操作就会启动
  187.     [_queueaddOperation:op1];
  188. }

  189. #pragma mark blockOperation
  190. - (IBAction)operationDemo2
  191. {
  192.     //block的最大好处,可以将一组相关的操作,顺序写在一起,便于调试以及代码编写
  193.     [_queueaddOperationWithBlock:^{
  194.         NSLog(@"%@", [NSThreadcurrentThread]);
  195.         
  196.         //模拟延时
  197.         [NSThreadsleepForTimeInterval:1.0f];
  198.         
  199.         //模拟获取到图像
  200.         UIImage*image = [UIImageimageNamed:@"头像1"];
  201.         
  202.         //设置图像,在主线程队列中设置
  203.         [[NSOperationQueuemainQueue]addOperationWithBlock:^{
  204.             _imageView.image= image;
  205.         }];
  206.     }];
  207. }

  208. #pragma mark模仿下载网络图像
  209. - (IBAction)operationDemo3:(id)sender
  210. {
  211.     // 1.下载
  212.     NSBlockOperation*op1 = [NSBlockOperationblockOperationWithBlock:^{
  213.         NSLog(@"下载%@", [NSThreadcurrentThread]);
  214.     }];
  215.     // 2.滤镜
  216.     NSBlockOperation*op2 = [NSBlockOperationblockOperationWithBlock:^{
  217.         NSLog(@"滤镜%@", [NSThreadcurrentThread]);
  218.     }];
  219.     // 3.显示
  220.     NSBlockOperation*op3 = [NSBlockOperationblockOperationWithBlock:^{
  221.         NSLog(@"更新UI %@", [NSThreadcurrentThread]);
  222.     }];
  223.     //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
  224.     //添加操作之间的依赖关系,所谓依赖关系,就是等待前一个任务完成后,后一个任务才能启动
  225.     //依赖关系可以跨线程队列实现
  226.     //提示:在指定依赖关系时,注意不要循环依赖,否则不工作。
  227.     [op2addDependency:op1];
  228.     [op3addDependency:op2];
  229. //    [op1 addDependency:op3];
  230.     
  231.     [_queueaddOperation:op1];
  232.     [_queueaddOperation:op2];
  233.     [[NSOperationQueuemainQueue]addOperation:op3];
  234. }

  235. #pragma mark限制线程数量
  236. - (IBAction)operationDemo4
  237. {
  238.     //控制同时最大并发的线程数量
  239.     [_queuesetMaxConcurrentOperationCount:2];
  240.     
  241.     for(NSIntegeri =0; i <200; i++) {
  242.         NSBlockOperation*op = [NSBlockOperationblockOperationWithBlock:^{
  243.             NSLog(@"%@", [NSThreadcurrentThread]);
  244.         }];
  245.         
  246.         [_queueaddOperation:op];
  247.     }
  248. }

  249. #pragma mark - GCD演练
  250. - (IBAction)gcdDemo1
  251. {
  252.     /**
  253.      GCD就是为了在多核上使用多线程技术
  254.      
  255.      1>要使用GCD,所有的方法都是dispatch开头的
  256.      2>名词解释
  257.         global 全局
  258.         queue  队列
  259.         async  异步
  260.         sync   同步
  261.      
  262.      3>要执行异步的任务,就在全局队列中执行即可
  263.         dispatch_async异步执行控制不住先后顺序
  264.      
  265.      4>关于GCD的队列
  266.         全局队列    dispatch_get_global_queue
  267.             参数:优先级DISPATCH_QUEUE_PRIORITY_DEFAULT
  268.                  始终是0
  269.         串行队列
  270.         主队列
  271.      
  272.      5>异步和同步于方法名无关,与运行所在的队列有关!
  273.         提示:要熟悉队列于同步、异步的运行节奏,一定需要自己编写代码测试!
  274.      
  275.         同步主要用来控制方法的被调用的顺序
  276.      */
  277.     // 1.队列
  278.     dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
  279.     
  280.     // 2.将任务异步(并发)执行
  281.     dispatch_async(queue, ^{
  282.         NSLog(@"a->%@", [NSThreadcurrentThread]);
  283.     });
  284.     dispatch_async(queue, ^{
  285.         NSLog(@"b->%@", [NSThreadcurrentThread]);
  286.     });
  287.     
  288.     dispatch_async(dispatch_get_main_queue(), ^{
  289.         NSLog(@"main - > %@", [NSThreadcurrentThread]);
  290.     });
  291. }

  292. @end
 

连续点击两次大任务按钮,运行结果

执行前-><NSThread: 0x764bcb0>{name = (null), num = 1}
执行后-><NSThread: 0x764bcb0>{name = (null), num = 1}
执行完毕
bigDemo<NSThread: 0x768d8f0>{name = (null), num = 3}
执行前-><NSThread: 0x764bcb0>{name = (null), num = 1}
bigDemo<NSThread: 0x755c8d0>{name = (null), num = 4}
执行后-><NSThread: 0x764bcb0>{name = (null), num = 1}
执行完毕

结论

performSelectorInBackground:withObject:该方法会使方法在后台运行,并负责为其分配线程号,一个线程3结束后,不会被回收.再次调用performSelectorInBackground:withObject:会分配4而非3

连续点击NSThread,运行结果

2013-12-27 19:06:01.311 1226-01-大任务[4742:51b] bigDemo<NSThread: 0x753d5d0>{name = (null), num = 3}
2013-12-27 19:06:06.266 1226-01-
大任务[4742:440b] bigDemo<NSThread: 0x753f1f0>{name = (null), num = 4}
2013-12-27 19:06:07.060 1226-01-
大任务[4742:4503] bigDemo<NSThread: 0x7540cb0>{name = (null), num = 5}
2013-12-27 19:06:07.659 1226-01-
大任务[4742:440f] bigDemo<NSThread: 0x753d9f0>{name = (null), num = 6}
2013-12-27 19:06:08.318 1226-01-
大任务[4742:4507] bigDemo<NSThread: 0x7540cb0>{name = (null), num = 7}

结论

initWithTarget:selector:object:方法能为方法分配线程

连续点击NSOperation按钮,运行结果

2013-12-27 19:24:16.153 1226-01-大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:18.755 1226-01-
大任务[4858:4203] <NSThread: 0x768acf0>{name = (null), num = 4}
2013-12-27 19:24:19.245 1226-01-
大任务[4858:1103] <NSThread: 0x768aa40>{name = (null), num = 5}
2013-12-27 19:24:19.724 1226-01-
大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:20.205 1226-01-
大任务[4858:4703] <NSThread: 0x768b7e0>{name = (null), num = 6}

结论

 addOperation:方法能为方法分配线程

连续点击NSBlockOperation按钮,运行结果

2013-12-27 19:24:16.153 1226-01-大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:18.755 1226-01-
大任务[4858:4203] <NSThread: 0x768acf0>{name = (null), num = 4}
2013-12-27 19:24:19.245 1226-01-
大任务[4858:1103] <NSThread: 0x768aa40>{name = (null), num = 5}
2013-12-27 19:24:19.724 1226-01-
大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:20.205 1226-01-
大任务[4858:4703] <NSThread: 0x768b7e0>{name = (null), num = 6}

结论

operationDemo1方法与operationDemo2方法效果一致

    //block的最大好处,可以将一组相关的操作,顺序写在一起,便于调试以及代码编写




限制并发线程数,运行结果

黄色部分是时间,结果发现:相同的时间只可能出现两次.

2013-12-27 20:00:58.088 1226-01-大任务[5127:1703] <NSThread: 0x754ad60>{name = (null), num = 4}
2013-12-27 20:00:58.088 1226-01-
大任务[5127:1103] <NSThread: 0x7667740>{name = (null), num = 3}
2013-12-27 20:00:58.092 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.093 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.096 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.096 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.099 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.100 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.101 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.102 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.105 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.106 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.107 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.107 1226-01-
大任务[5127:4107] <NSThread: 0x7530f50>{name = (null), num = 6}
2013-12-27 20:00:58.125 1226-01-
大任务[5127:1703] <NSThread: 0x754ad60>{name = (null), num = 4}
2013-12-27 20:00:58.126 1226-01-
大任务[5127:4107] <NSThread: 0x7530f50>{name = (null), num = 6}
2013-12-27 20:00:58.129 1226-01-
大任务[5127:1103] <NSThread: 0x7667740>{name = (null), num = 3}
2013-12-27 20:00:58.129 1226-01-
大任务[5127:1703] <NSThread: 0x754ad60>{name = (null), num = 4}
2013-12-27 20:00:58.132 1226-01-
大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}

结论

setMaxConcurrentOperationCount 方法能控制同时最大并发的线程数量

运行两次程序,并在每次运行时连续点击gcd按钮三次,运行结果

——————————————————— <第⓵次运行gcd>———————————————————————

——————————————————— <第⓵点击gcd按钮>———————————————————————
2013-12-27 20:23:49.987 1226-01-大任务[5270:907] #########main - ><NSThread: 0x75652f0>{name = (null), num = 1}
2013-12-27 20:23:49.988 1226-01-大任务[5270:1103] ###########################-><NSThread: 0x75848f0>{name = (null), num = 3}
2013-12-27 20:23:49.987 1226-01-大任务[5270:1903] ###################-><NSThread: 0x76bfb20>{name = (null), num = 4}
——————————————————— <第⓶点击gcd按钮>———————————————————————
2013-12-27 20:23:54.336 1226-01-大任务[5270:907] #########main - ><NSThread: 0x75652f0>{name = (null), num = 1}
2013-12-27 20:23:54.337 1226-01-大任务[5270:1103] ###########################-><NSThread: 0x75848f0>{name = (null), num = 3}
2013-12-27 20:23:54.337 1226-01-大任务[5270:1103] ###################-><NSThread: 0x75848f0>{name = (null), num = 3}
——————————————————— <第⓷点击gcd按钮>———————————————————————
2013-12-27 20:23:59.435 1226-01-大任务[5270:907] #########main - ><NSThread: 0x75652f0>{name = (null), num = 1}
2013-12-27 20:23:59.436 1226-01-大任务[5270:1103] ###########################-><NSThread: 0x75848f0>{name = (null), num = 3}
2013-12-27 20:23:59.437 1226-01-大任务[5270:1103] ###################-><NSThread: 0x75848f0>{name = (null), num = 3}

——————————————————— <第⓶次运行gcd>———————————————————————
——————————————————— <第⓵点击gcd按钮>———————————————————————
2013-12-27 20:24:49.950 1226-01-大任务[5288:907] #########main - ><NSThread: 0x764b3e0>{name = (null), num = 1}
2013-12-27 20:24:49.950 1226-01-大任务[5288:1a03] ###################-><NSThread: 0x76911d0>{name = (null), num = 4}
2013-12-27 20:24:49.949 1226-01-大任务[5288:1103] ###########################-><NSThread: 0x7687e20>{name = (null), num = 3}
——————————————————— <第⓶点击gcd按钮>———————————————————————
2013-12-27 20:24:53.798 1226-01-大任务[5288:907] #########main - ><NSThread: 0x764b3e0>{name = (null), num = 1}
2013-12-27 20:24:53.798 1226-01-大任务[5288:1a03] ###################-><NSThread: 0x76911d0>{name = (null), num = 4}
2013-12-27 20:24:53.798 1226-01-大任务[5288:1103] ###########################-><NSThread: 0x7687e20>{name = (null), num = 3}
——————————————————— <第⓷点击gcd按钮>———————————————————————
2013-12-27 20:24:55.320 1226-01-大任务[5288:907] #########main - ><NSThread: 0x764b3e0>{name = (null), num = 1}
2013-12-27 20:24:55.321 1226-01-大任务[5288:1a03] ###################-><NSThread: 0x76911d0>{name = (null), num = 4}
2013-12-27 20:24:55.321 1226-01-大任务[5288:1103] ###########################-><NSThread: 0x7687e20>{name = (null), num = 3}


结论

  1. 要执行异步的任务,就在全局队列dispatch_get_global_queue中执行即可
  2. dispatch_async异步执行控制不住先后顺序

 //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html

https://www.evernote.com/shard/s227/sh/911b8ec7-498f-4c93-b180-6713e5e03931/382ccb2b15dd2630ddd9756e48fc1446


作者:
出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
本文版权归作者和博客园共有,欢迎转载,
但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/ChenYilong/p/3494810.html