首页 技术 正文
技术 2022年11月6日
0 收藏 342 点赞 815 浏览 12883 个字

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

 

  1. iOS多线程 
iOS开发Demo(示例程序)源代码
  2. iOS程序源代码下载链接:
    iOS多线程 
iOS开发Demo(示例程序)源代码01.大任务.zip
    225.8 KB
  3. //
  4. //  ViewController.m
  5. //  01.大任务
  6. //
  7. //  Created by apple on 13-12-27.
  8. //  Copyright (c) 2013年itcast. All rights reserved.
  9. //
  10. #import”ViewController.h”
  11. @interfaceViewController()
  12. {
  13.     //全局的操作队列,由它统一控制所有的NSOperation的操作调度
  14.     NSOperationQueue        *_queue;
  15. }
  16. @property(weak,nonatomic)IBOutletUIImageView*imageView;
  17. @end
  18. @implementationViewController
  19. /**
  20.  无论使用哪种多线程技术都可以使用
  21.  [NSThread currentThread]跟踪查看当前执行所在的线程情况。
  22.  num = 1表示在主线程上执行的任务
  23.  
  24.  ================================================================
  25.  1. NSObject多线程技术
  26.  
  27.  1>使用performSelectorInBackground可以开启后台线程,执行selector选择器选择的方法
  28.  2>使用performSelectorOnMainThread可以重新回到主线程执行任务,通常用于后台线程更新界面UI时使用
  29.  3> [NSThread sleepForTimeInterval:1.0f];
  30.     让当前线程休眠,通常在程序开发中,用于模拟耗时操作,以便跟踪不同的并发执行情况!
  31.  
  32.     但是:在程序发布时,千万不要保留此方法!不要把测试中的代码交给客户,否则会造成不好的用户体验。
  33.  
  34.  提示:使用performSelectorInBackground也可以直接修改UI,但是强烈不建议使用。
  35.  
  36.  注意:在使用NSThread或者NSObject的线程方法时,一定要使用自动释放池,否则容易出现内存泄露。
  37.  
  38.  ================================================================
  39.  2. NSThread的多线程技术
  40.  
  41.  1>类方法直接开启后台线程,并执行选择器方法
  42.     detachNewThreadSelector
  43.  
  44.  2>成员方法,在实例化线程对象之后,需要使用start执行选择器方法
  45.     initWithTarget
  46.  
  47.  对于NSThread的简单使用,可以用NSObject的performSelectorInBackground替代
  48.  
  49.  同时,在NSThread调用的方法中,同样要使用autoreleasepool进行内存管理,否则容易出现内存泄露。
  50.  //转载请注明出处–本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
  51.  ================================================================
  52.  3. NSOperation,面向对象的多线程技术
  53.  
  54.  1>使用步骤:
  55.     1)实例化操作
  56.         a) NSInvocationOperation
  57.         b) NSBlockOperation
  58.     2)将操作添加到队列NSOperationQueue即可启动多线程执行
  59.  
  60.  2>更新UI使用主线程队列
  61.     [NSOpeationQueue mainQueue] addOperation ^{};
  62.  
  63.  3>操作队列的setMaxConcurrentOperationCount
  64.     可以设置同时并发的线程数量!
  65.  
  66.     提示:此功能仅有NSOperation有!
  67.  
  68.  4>使用addDependency可以设置任务的执行先后顺序,同时可以跨操作队列指定依赖关系
  69.  
  70.     提示:在指定依赖关系时,注意不要循环依赖,否则不工作。
  71.  //转载请注明出处–本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
  72.  ================================================================
  73.  4. GCD,C语言
  74.  
  75.  */
  76. – (void)viewDidLoad
  77. {
  78.     [superviewDidLoad];
  79.     
  80.     NSLog(@”%@”, [NSThreadcurrentThread]);
  81.     
  82.     //实例化操作队列
  83.     _queue= [[NSOperationQueuealloc]init];
  84. }
  85. #pragma mark -操作
  86. //耗时操作演示
  87. – (void)bigDemo
  88. {
  89.     //自动释放池
  90.     //负责其他线程上的内存管理,在使用NSThread或者NSObject的线程方法时,一定要使用自动释放池
  91.     //否则容易出现内存泄露。
  92.     @autoreleasepool{
  93.         //    //模拟网络下载延时
  94.         //    for (NSInteger i = 0; i < 1000; i++) {
  95.         //        NSString *str = [NSString stringWithFormat:@”%d”, i];
  96.         //
  97.         //        //提示:NSLog是非常耗时的操作!
  98.         //        NSLog(@”大任务-> %@”, str);
  99.         //    }
  100.         
  101.         NSLog(@”%@”, [NSThreadcurrentThread]);
  102.         //模拟网络下载延时,睡眠1秒,通常是在开发中测试使用。
  103.         [NSThreadsleepForTimeInterval:1.0f];
  104.         
  105.         //强烈不建议直接在后台线程更新界面UI!
  106.         //模拟获取到下载的图像
  107.         UIImage*image = [UIImageimageNamed:@”头像1″];
  108.         
  109.         //在主线程更新图像
  110.         //使用self调用updateImage方法在主线程更新图像
  111.         //    [self performSelectorOnMainThread:@selector(updateImage:) withObject:image waitUntilDone:YES];
  112.         
  113.         //使用imageView的setImage方法在主线程更新图像
  114.         [_imageViewperformSelectorOnMainThread:@selector(setImage:)withObject:imagewaitUntilDone:YES];
  115.         
  116.         // ….
  117.     }
  118. }
  119. #pragma mark -更新图像,模拟从网络上下载完图片后,更新界面的操作
  120. – (void)updateImage:(UIImage*)image
  121. {
  122.     NSLog(@”更新图像-> %@”, [NSThreadcurrentThread]);
  123.     
  124.     _imageView.image= image;
  125. }
  126. #pragma mark – Actions
  127. – (IBAction)bigTask
  128. {
  129.     //本方法中的所有代码都是在主线程中执行的
  130.     // NSObject多线程技术
  131.     NSLog(@”执行前->%@”, [NSThreadcurrentThread]);
  132.     
  133.     // performSelectorInBackground是将bigDemo的任务放在后台线程中执行
  134.     [selfperformSelectorInBackground:@selector(bigDemo)withObject:nil];
  135.     
  136.     NSLog(@”执行后->%@”, [NSThreadcurrentThread]);
  137. //    [self bigDemo];
  138.     
  139.     NSLog(@”执行完毕”);
  140. }
  141. – (IBAction)smallTask
  142. {
  143.     NSString*str =nil;
  144.     
  145.     for(NSIntegeri =0; i <50000; i++) {
  146.         str = [NSStringstringWithFormat:@”%d”, i];
  147.     }
  148.     
  149.     NSLog(@”小任务-> %@”, str);
  150. }
  151. #pragma mark NSThread演练
  152. – (IBAction)threadDemo
  153. {
  154.     //新建一个线程,调用@selector方法
  155. //    [NSThread detachNewThreadSelector:@selector(bigDemo) toTarget:self withObject:nil];
  156.     
  157.     //成员方法
  158.     NSThread*thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(bigDemo)object:nil];
  159.     
  160.     //启动start线程
  161.     [threadstart];
  162. }
  163. #pragma mark – NSOperation演练
  164. – (void)opAction
  165. {
  166.     NSLog(@”%@”, [NSThreadcurrentThread]);
  167.     
  168.     //模拟延时
  169.     [NSThreadsleepForTimeInterval:1.0f];
  170.     
  171.     //模拟获取到图像
  172.     UIImage*image = [UIImageimageNamed:@”头像1″];
  173.     
  174.     //设置图像,在主线程队列中设置
  175.     [[NSOperationQueuemainQueue]addOperationWithBlock:^{
  176.         _imageView.image= image;
  177.     }];
  178. }
  179. #pragma mark invocation
  180. – (IBAction)operationDemo1
  181. {
  182.     NSInvocationOperation*op1 = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(opAction)object:nil];
  183.     
  184.     //如果使用start,会在当前线程启动操作
  185. //    [op1 start];
  186.     
  187.     // 1.一旦将操作添加到操作队列,操作就会启动
  188.     [_queueaddOperation:op1];
  189. }
  190. #pragma mark blockOperation
  191. – (IBAction)operationDemo2
  192. {
  193.     //用block的最大好处,可以将一组相关的操作,顺序写在一起,便于调试以及代码编写
  194.     [_queueaddOperationWithBlock:^{
  195.         NSLog(@”%@”, [NSThreadcurrentThread]);
  196.         
  197.         //模拟延时
  198.         [NSThreadsleepForTimeInterval:1.0f];
  199.         
  200.         //模拟获取到图像
  201.         UIImage*image = [UIImageimageNamed:@”头像1″];
  202.         
  203.         //设置图像,在主线程队列中设置
  204.         [[NSOperationQueuemainQueue]addOperationWithBlock:^{
  205.             _imageView.image= image;
  206.         }];
  207.     }];
  208. }
  209. #pragma mark模仿下载网络图像
  210. – (IBAction)operationDemo3:(id)sender
  211. {
  212.     // 1.下载
  213.     NSBlockOperation*op1 = [NSBlockOperationblockOperationWithBlock:^{
  214.         NSLog(@”下载%@”, [NSThreadcurrentThread]);
  215.     }];
  216.     // 2.滤镜
  217.     NSBlockOperation*op2 = [NSBlockOperationblockOperationWithBlock:^{
  218.         NSLog(@”滤镜%@”, [NSThreadcurrentThread]);
  219.     }];
  220.     // 3.显示
  221.     NSBlockOperation*op3 = [NSBlockOperationblockOperationWithBlock:^{
  222.         NSLog(@”更新UI %@”, [NSThreadcurrentThread]);
  223.     }];
  224.     //转载请注明出处–本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
  225.     //添加操作之间的依赖关系,所谓“依赖”关系,就是等待前一个任务完成后,后一个任务才能启动
  226.     //依赖关系可以跨线程队列实现
  227.     //提示:在指定依赖关系时,注意不要循环依赖,否则不工作。
  228.     [op2addDependency:op1];
  229.     [op3addDependency:op2];
  230. //    [op1 addDependency:op3];
  231.     
  232.     [_queueaddOperation:op1];
  233.     [_queueaddOperation:op2];
  234.     [[NSOperationQueuemainQueue]addOperation:op3];
  235. }
  236. #pragma mark限制线程数量
  237. – (IBAction)operationDemo4
  238. {
  239.     //控制同时最大并发的线程数量
  240.     [_queuesetMaxConcurrentOperationCount:2];
  241.     
  242.     for(NSIntegeri =0; i <200; i++) {
  243.         NSBlockOperation*op = [NSBlockOperationblockOperationWithBlock:^{
  244.             NSLog(@”%@”, [NSThreadcurrentThread]);
  245.         }];
  246.         
  247.         [_queueaddOperation:op];
  248.     }
  249. }
  250. #pragma mark – GCD演练
  251. – (IBAction)gcdDemo1
  252. {
  253.     /**
  254.      GCD就是为了在“多核”上使用多线程技术
  255.      
  256.      1>要使用GCD,所有的方法都是dispatch开头的
  257.      2>名词解释
  258.         global 全局
  259.         queue  队列
  260.         async  异步
  261.         sync   同步
  262.      
  263.      3>要执行异步的任务,就在全局队列中执行即可
  264.         dispatch_async异步执行控制不住先后顺序
  265.      
  266.      4>关于GCD的队列
  267.         全局队列    dispatch_get_global_queue
  268.             参数:优先级DISPATCH_QUEUE_PRIORITY_DEFAULT
  269.                  始终是0
  270.         串行队列
  271.         主队列
  272.      
  273.      5>异步和同步于方法名无关,与运行所在的队列有关!
  274.         提示:要熟悉队列于同步、异步的运行节奏,一定需要自己编写代码测试!
  275.      
  276.         同步主要用来控制方法的被调用的顺序
  277.      */
  278.     // 1.队列
  279.     dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
  280.     
  281.     // 2.将任务异步(并发)执行
  282.     dispatch_async(queue, ^{
  283.         NSLog(@”a->%@”, [NSThreadcurrentThread]);
  284.     });
  285.     dispatch_async(queue, ^{
  286.         NSLog(@”b->%@”, [NSThreadcurrentThread]);
  287.     });
  288.     
  289.     dispatch_async(dispatch_get_main_queue(), ^{
  290.         NSLog(@”main – > %@”, [NSThreadcurrentThread]);
  291.     });
  292. }
  293. @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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291